在 Fedora Linux 系统上安装 PowerShell
PowerShell
![](/data/attachment/album/202212/03/120749nkevgkb12exbeffg.jpg)


PowerShell（也可写作 pwsh）是一个功能强大的开源命令行工具，它是面向对象的，由微软开发和维护。它的语法特征冗长，但对用户来说比较直观。本文介绍如何在主机上和在 Podman 或其他容器内安装 PowerShell。


### 为何使用 PowerShell


PowerShell，正如它的名字那样，是一个强大的工具。它的句法冗长，但语义清晰。对那些不愿意写长命令的开发者来说，PowerShell 的大多数命令都有别名。可以使用 `Get-Alias` 或点击 [此处](https://ilovepowershell.com/2011/11/03/list-of-top-powershell-alias/) 查询别名的使用方法。


PowerShell 和传统的 Shell 最大的区别在于它的输出管道。普通的 Shell 输出的是字符串或字符流，PowerShell 输出的是对象。这对命令管道的工作方式具有深远的影响，而且它具有很多的优点。


#### 演示例子


下面的例子体现的是冗长而清晰的特点。以 `#` 号开头的行是注释行。以 `PS >` 开头的行是命令行，`PS >` 是提示符：



```
# Return all files greater than 50MB in the current directory.
## Longest form
PS > Get-Childitem | Where-Object Length -gt 50MB
## Shortest form (with use of aliases)
PS > gci | ? Length -gt 40MB
## Output looks like this
    Directory: /home/Ozymandias42/Downloads
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----          20/08/2020    13:55     2000683008 40MB-file.img


# In order: get VMs, get snapshots, only select the last 3 and remove selected list:
PS > Get-VM VM-1 | Get-Snapshot | Select-Object -Last 3 | Remove-Snapshot

```

上述例子说明了：Bash 脚本经常需要用 `cut`、`sed`、`awk` 等工具对输入/输出进行格式化，而使用 PowerShell 时通常就没有这个必要了。这是因为 PowerShell 的工作机制跟传统的 POSIX shell（例如 Bash、Zsh、Fish等）有本质的不同。传统的 Shell 的命令输出形式是字符串，而在 PowerShell 中，命令输出形式为对象。


#### Bash 与 PowerShell 的比较


下面的例子说明了与 Bash 中的字符串输出模式相比，PowerShell 的对象输出模式的优点。假设你需要写一段脚本，该脚本的作用显示所有进程，这些进程一共占用了 200MB 内存空间。如果使用 Bash，大致如下：



```
$ ps -eO rss | awk -F' ' \
    '{ if($2 >= (1024*200)) { \
        printf("%s\t%s\t%s\n",$1,$2,$6);} \
     }'

PID    RSS     COMMAND
A      B       C
[...]

```

第一个显而易见的差别就是可读性，或更确切地说是语义清晰度。 `ps` 和 `awk` 都不是自描述的。`ps` 命令的功能是显示进程状态；`awk` 是一种文本处理工具和语言，这个词汇每个字母都是前期开发人员的名字（**A**ho, **W**einberger, **K**ernighan（详见 [维基百科](https://en.wikipedia.org/wiki/AWK)）的首字母。然而，在把它与 PowerShell 作比较前，先看看这个脚本：


* `ps -e` 输出所有运行中的进程；
* `-O rss` 输出 `ps` 的默认输出内容，再加上 RSS 字段 —— 每个进程使用的千字节数（以 KB 为单位）；输出结果类似于：



```
PID  RSS   S TTY TIME     COMMAND  
1    13776 S ?   00:00:01 /usr/lib/systemd/systemd

```
* `|` 管道操作符使用左边命令的输出作为右边命令的输入。
* `awk -F' '` 定义“空格”，作为输入字段分隔符。以上面的例子来说，PID 是第一个字段，RSS 是第二个字段，依此类推。
* `'{ if($2 >= (1024*200)) {` 是实际的 AWK 代码起始处。它的作用是检查第二个字段（[RSS](https://en.wikipedia.org/wiki/Resident_set_size)）是否包含一个大于或等于 1024\*200 的数字；
* `printf(“%s\t%s\t%s\n”,$1,$2,$6);}` 脚本继续。如果前面的条件成立，则输出第一、第二和第六个字段（分别是 [PID](https://en.wikipedia.org/wiki/Process_identifier)、[RSS](https://en.wikipedia.org/wiki/Resident_set_size) 和 `COMMAND` 字段）。


考虑到这一点，退一步说，编写这段脚本需要什么才能令它工作：


* 输入命令 `ps` 的输出中必须包含我们想要过滤的字段。这在默认情况下是没有的，需要我们使用 `-O` 标志和 `rss` 字段作为参数。
* 我们需要将 `ps` 的输出当作一组输入字段，所以我们还应当知道它们的顺序和结构。换句话说，我们至少需要确定 `RSS` 是第二个字段。这也意味着我们需要提前知道 `ps` 的输出信息的大致情况。
* 然后我们需要知道过滤的数据是什么单位，以及相关工具的单位是什么。也就是我们需要知道 `RSS` 和 `awk` 字段使用 kb。不然我们就不能写出 `($2 >= 1024*200)` 这样的表达式。


现在，我们把前面的命令跟 PoserShell 中等价的命令比较：



```
# Longest form
PS > Get-Process | Where-Object WorkingSet -ge 200MB
# Shortest form (with use of aliases)
PS > gps | ? ws -ge 200MB

NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
------    -----      -----     ------      --  -- -----------
     A        B          C          D       E   F           G
[...]

```

首先应该注意到，语义非常清晰。这些命令都是自描述的，能清晰描述它们做什么。


此外，不需要对输入-输出重新格式化，也不需要关心输入命令使用的单位。这是因为 PowerShell 输出的是对象，而非字符串。


考虑下述情况，就可以理解这些内容。在 Bash 中，命令的输出信息就是终端显示的信息。在 PowerShell 中，终端显示的信息并不等于实际可用的信息。这是由于 PowerShell 中的输出-打印系统使用的也是对象。因此 PowerShell 中每一条命令都对输出的对象的一些属性作了可打印的标记，也对一些属性作了不可打印的标记。然而，它总是包括所有的属性，而 Bash 只包括它实际打印的内容。我们可以把它想象成 JSON 对象。Bash 中的输出位置被分为一些“字段”，以空格或制表符为标志，在 PowerShell 中它是一个容易寻址的对象属性，只需要知道它的名称即可使用。就像上述例子中的 `WorkingSet` 那样。


为了看到一条命令的输出对象的所有属性和它们的类型，可以进行以下操作:



```
PS > Get-Process | Get-Member

```

### 安装 PowerShell


PowerShell 安装包的形式有若干种，包括 Fedora Linux 中使用的 RPM 安装包。本文介绍在 Fedora Linux 中如何使用多种方法安装 PowerShell。


我推荐使用原生的方法安装。但我也会介绍如何在容器中安装。我将展示使用官方微软 PowerShell 容器和 Fedora Linux 30 的 Toolbox 容器。使用容器的优点在于，所有的依赖捆绑在其中，并且与主机隔离，所以它一定是有效的。无论如何，虽然官方文档只是明确指出支持 Fedora Linux 发行版的 28-30 版本，我还是建议使用原生的方法安装。


**注意：** 官方支持意味着一定有效。但其他的版本也不是一定不兼容。也就是说，高于 30 的发行版也应该有效。经过测试，的确如此。


在容器中设置并运行 PowerShell 比直接在主机上运行它难度更大，安装需要花费更多时间，而且你还不能直接运行主机的命令。


#### 在主机上使用包管理器安装 PowerShell


##### 方法一：使用微软仓库


安装过程很直接，而且跟通过第三方仓库安装其他软件没什么区别。


通俗地说，安装过程可以分为四步：


1. 添加新仓库的 GPG 密码
2. 在 DNF 仓库列表中新增相应的仓库
3. 刷新 DNF 缓存，将新仓库中的有关包包含进来
4. 安装新包


然后使用命令 `pwsh` 启动 PowerShell。



```
$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
$ curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
$ sudo dnf makecache
$ sudo dnf install powershell
$ pwsh

```

欲删除仓库和包，运行以下命令：



```
$ sudo rm /etc/yum.repos.d/microsoft.repo
$ sudo dnf remove powershell

```

##### 方法 2：使用 PRM 文件


这种方法与第一种方法没有明显的差别。实际上，在安装 RPM 文件时，隐式添加了 GPG 密码和仓库。这是由于 RPM 文件包含它们两者的关联关系，保存在它的元数据中。


首先，从 [PowerShell GitHub 仓库](https://github.com/PowerShell/PowerShell) 获取相应版本的 `.rpm` 文件。然后查看 `readme.md` 文件中的 “获取 PowerShell” 部分的内容。


第二步，输入以下命令：



```
$ sudo dnf install powershell-<version>.rhel.7.<architecture>.rpm

```

在 `<version>` 和 `<architecture>` 节点中填写各自的内容，例如 [powershell-7.1.3-1.rhel.7.x86\_64.rpm](https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/powershell-7.1.3-1.rhel.7.x86_64.rpm)。


你也可以使用链接运行它，不指定版本和架构，先把它下载到本地。



```
$ sudo dnf install https://github.com/PowerShell/PowerShell/releases/download/v<version>/powershell-<version>.rhel.7.<architecture>.rpm

```

欲删除 PowerShell，运行以下命令：



```
$ sudo dnf remove powershell

```

#### 通过容器安装


##### 方法一：使用 Podman 容器


Podman 是一个兼容 [开放容器倡议](https://opencontainers.org/)（OCI）的、嵌入式的容器引擎，它可以代替 Docker。


微软提供了 [PowerShell Docker 容器集成工具](https://hub.docker.com/_/microsoft-powershell)。下面的例子将在 Podman 中使用容器。


欲了解更多关于 Podman 的信息，可以访问 [Podman.io](https://podman.io/)。Fedora 杂志还有一个专为 Podman 设计的 [标签](https://fedoramagazine.org/tag/podman/)。


欲在 Podman 中使用 PowerShell，运行以下脚本：



```
$ podman run \
  -it \
  --privileged \
  --rm \
  --name powershell \
  --env-host \
  --net=host --pid=host --ipc=host \
  --volume $HOME:$HOME \
  --volume /:/var/host \
  mcr.microsoft.com/powershell \
  /usr/bin/pwsh \
  -WorkingDirectory $(pwd)

```

这段脚本为使用 PowerShell 创建了一个 Podman 容器，并立即接入其中。它还将 `/home` 和主机的根目录挂载到容器中，确保它们在容器中是可用的。无论如何，在 `/var/host` 目录下，主机的根目录是可访问的。


但是，在容器内部，你只能间接运行主机命令。有一种变通办法，就是先运行 `chroot /var/host` 改变根目录，然后运行主机命令。


为了把命令拆分开来讲解，除非特别指定，以下所有内容都是强制性的：


* `-it` 创建一个持久环境，当你进入该环境后，不会轻易退出；
* `--privileged` 给予容器扩展的权限（可选）；
* `--rm` 当你退出时移除容器；
* `--name` 设置容器名称；
* `--env-host` 将所有主机的环境变量设置为容器的变量（可选）；
* `--net=host --pid=host --ipc=host` 在主机的命名空间中运行进程（而非一组单独的名称空间）；
* `--volume $HOME:$HOME` 挂载用户目录；
* `--volume /:/var/host` 将主机根目录挂载到 `/var/host`（可选）；
* `mcr.microsoft.com/powershell` 进入容器；
* `/usr/bin/pwsh` 可选但很方便的参数：用别名 `pwsh`，脚本中有了它，可以输入 `pwsh` 轻松访问 Podman 容器；
* `-WorkingDirectory $(pwd)` 在当前目录下进入容器（可选）。


欲移除 PowerShell 镜像，运行以下命令：



```
$ podman rmi mcr.microsoft.com/powershell

```

##### 方法二：Fedora 系统的 Toolbox 容器


在不影响主机系统的情况下安装持久化环境，使用 Toolbox 容器是一种巧妙的解决方案。它充当了 Podman 的封装器，负责提供大量的标志，就像方法一中提到的那样。因此，Toolbox 比 Podman 容易使用。它可以用来开发和调试。有了 Toolbox，你可以运行任何命令，跟你直接在 Fedora 工作站主机上运行是一样的。


安装步骤跟在主机上安装一样，唯一的区别就是在容器内部进行。你需要确保已经安装了 `toolbox` 包。


使用 Fedora 34 Toolbox 容器需要两个步骤：


1. 创建 Fedora 34 Toolbox 容器
2. 运行 Fedora 34 Toolbox 容器



```
$ toolbox create --image registry.fedoraproject.org/f34/fedora-toolbox
$ toolbox enter --container fedora-toolbox

```

接着，按照 [方法一：使用微软仓库](file:///Users/xingyuwang/develop/TranslateProject-wxy/translated/tech/tmp.c7U2gcu9Hl#method-1-microsoft-repositories) 中的相关内容操作。


可选但非常方便的做法：使用别名 `pwsh`，可以轻松地访问 Toolbox 容器：



```
toolbox run –container fedora-toolbox pwsh

```

欲移除 Toolbox 容器，需要确保你已经使用 `exit` 关闭了 Toolbox 会话，然后运行以下命令：



```
$ podman kill fedora-toolbox
$ toolbox rm fedora-toolbox

```



---


via: <https://fedoramagazine.org/install-powershell-on-fedora-linux/>


作者：[TheEvilSkeleton, Ozymandias42](https://fedoramagazine.org/author/theevilskeleton/https://fedoramagazine.org/author/ozymandias42/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[cool-summer-021](https://github.com/cool-summer-021) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
PowerShell (also written pwsh) is a powerful open source command-line and object-oriented shell developed and maintained by Microsoft. It is syntactically verbose and intuitive for the user. This article is a guide on how to install PowerShell on the host and inside a Podman or Toolbox container.

## Table of contents

## Why use PowerShell

PowerShell, as the name suggests, is *power*ful. The syntax is verbose and semantically clear to the end user. For those that don’t want to write long commands all the time, most commands are aliased. The aliases can be viewed with *Get-Alias* or [here](https://ilovepowershell.com/2011/11/03/list-of-top-powershell-alias/).

The most important difference between PowerShell and traditional shells, however, is its output pipeline. While normal shells output strings or character streams, PowerShell outputs objects. This has far reaching implications for how command pipelines work and comes with quite a few advantages.

### Demonstration

The following examples illustrate the verbosity and simplicity. Lines that start with the pound symbol (**#**) are comments. Lines that start with **PS >** are commands, **PS >** being the prompt:

# Return all files greater than 50MB in the current directory. ## Longest formPS > Get-Childitem | Where-Object Length -gt 50MB## Shortest form (with use of aliases)PS > gci | ? Length -gt 40MB## Output looks like this Directory: /home/Ozymandias42/Downloads Mode LastWriteTime Length Name ---- ------------- ------ ---- ----- 20/08/2020 13:55 2000683008 40MB-file.img # In order: get VMs, get snapshots, only select the last 3 and remove selected list:PS > Get-VM VM-1 | Get-Snapshot | Select-Object -Last 3 | Remove-Snapshot

What this shows quite well is that input-output reformatting with tools like *cut*, *sed*, *awk* or similar, which Bash scripts often need, is usually not necessary in PowerShell. The reason for this is that PowerShell works fundamentally different than traditional POSIX shells such as Bash, Zsh, or other shells like Fish. The commands of traditional shells are output as strings whereas in PowerShell they are output as objects.

### Comparison between Bash and PowerShell

The following example illustrates the advantages of the object-output in PowerShell in contrast to the traditional string-output in Bash. Suppose you want a script that outputs all processes that occupy 200MB or more in RAM. With Bash, this might look something like this:

$ps -eO rss | awk -F' ' \'{ if($2 >= (1024*200)) { \printf("%s\t%s\t%s\n",$1,$2,$6);} \}'PID RSS COMMAND A B C [...]

The first obvious difference is readability or more specifically, semantic clarity. Neither *ps* nor *awk* are self-descriptive. *ps* shows the **p**rocess** s**tatus and *awk* is a text processing tool and language whose letters are the initials of its developers’ last names, **A**ho, **W**einberger, **K**ernighan (see [Wikipedia](https://en.wikipedia.org/wiki/AWK)). Before contrasting it with PowerShell however, examine the script:

*ps -e*outputs all running processes;*-O rss*outputs the default output of*ps*plus the amount of kilobytes each process uses, the*rss*field; this output looks like this:PID RSS S TTY TIME COMMAND 1 13776 S ? 00:00:01 /usr/lib/systemd/systemd

- | pipe operator uses the output of the command on the left side as input for the command on the right side.
*awk -F’ ‘*declares “space” as the input field separator. So going with the above example, PID is the first, RSS the second and so on.*‘{ if($2 >= (1024*200)*is the beginning of the actual AWK-script. It checks whether field 2 ([RSS](https://en.wikipedia.org/wiki/Resident_set_size)) contains a number larger than or equal to 1024*200KB (204800KB, or 200MB);*{ printf(“%s\t%s\t%s\n”,$1,$2,$6);} }’*continues the script. If the previous part evaluates to true, this outputs the first, second and sixth field ([PID](https://en.wikipedia.org/wiki/Process_identifier),[RSS](https://en.wikipedia.org/wiki/Resident_set_size)and COMMAND fields respectively).

With this in mind, step back and look at what was required for this script to be written and for it to work:

- The input command
*ps*had to have the field we wanted to filter against in its output. This was not the case by default and required us to use the*-O*flag with the*rss*field as argument. - We had to treat the output of
*ps*as a list of input fields, requiring us to know their order and structure. Or in other words, we had to at least*know*that*RSS*would be the second field. Meaning we had to know how the output of*ps*would look beforehand. - We then had to know what unit the data we were filtering against was in as well as what unit the processing tool would work in. Meaning we had to know that the
*RSS*field uses kilobytes and that*awk*does too. Otherwise we would not have been able to write the expression*($2 <= 1024*200)*

Now, contrast the above with the PowerShell equivalent:

# Longest formPS > Get-Process | Where-Object WorkingSet -ge 200MB# Shortest form (with use of aliases)PS > gps | ? ws -ge 200MBNPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- A B C D E F G [...]

This first thing to notice is that we have perfect semantic clarity. The commands are perfectly self-descriptive in what they do.

Furthermore there is no requirement for input-output reformatting, nor is there concern about the unit used by the input command. The reason for this is that PowerShell does not output strings, but objects.

To understand this think about the following. In Bash the output of a command is equal to that what it prints out in the terminal. In PowerShell what is printed on the terminal is not equal to the information, that is actually available. This is, because the output-printing system in PowerShell also works with objects. So every command in PowerShell marks some of the properties of its output objects as printable and others not. However, it always includes all properties, whereas Bash only includes what it actually prints. One can think of it like JSON objects. Where output in Bash would be separated into “fields” by a delimiter such as a space or tab, it becomes an easily addressable object property in PowerShell, with the only requirement being, that one has to know its name. Like *WorkingSet* in the above example.

To see all available properties of a command’s output objects and their types, one can simply do something like:

PS > Get-Process | Get-Member

## Install PowerShell

PowerShell is available in several package formats, including RPM used by Fedora Linux. This article shows how to install PowerShell on Fedora Linux using various methods.

I recommend installing it natively. But I will also show how to do it in a container. I will show using both the official Microsoft PowerShell container and a Fedora Linux 30 toolbox container. The advantage of the container-method is that it’s guaranteed to work, since all dependencies are bundled in it, and isolation from the host. Regardless, I recommend doing it natively, despite the official docs only explicitly stating Fedora Linux releases 28 to 30 as being supported.

**Note:** Supported means guaranteed to work. It does not necessarily mean incompatible with other releases. This means, that while not guaranteed, releases higher than 30 should still work. They did in fact work in our tests.

It is more difficult to set up PowerShell and run it in a container than to run it directly on a host. It takes more time to install and you will not be able to run host commands directly.

### Install PowerShell on a host using the package manager

#### Method 1: Microsoft repositories

Installation is as straight-forward as can be and the procedure doesn’t differ from any other software installed through third party repositories.

It can be split into four general steps:

- Adding the new repository’s GPG key
- Adding repository to DNF repository list
- Refreshing DNF cache to include available packages from the new repository
- Installing new packages

Powershell is then launched with the command *pwsh*.

$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc $ curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo $ sudo dnf makecache $ sudo dnf install powershell $ pwsh

To remove the repository and packages, run the following.

$ sudo rm /etc/yum.repos.d/microsoft.repo $ sudo dnf remove powershell

#### Method 2: RPM file

This method is not meaningfully different from the first method. In fact it adds the GPG key and the repository implicitly when installing the RPM file. This is because the RPM file contains the link to both in it’s metadata.

First, get the *.rpm* file for the version you want from the [PowerShell Core GitHub repository](https://github.com/PowerShell/PowerShell). See the readme.md

“Get Powershell” table for links.

Second, enter the following:

$ sudo dnf install powershell-<version>.rhel.7.<architecture>.rpm

Substitute *<version>* and *<architecture>* with the version and architecture you want to use respectively, for example [powershell-7.1.3-1.rhel.7.x86_64.rpm](https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/powershell-7.1.3-1.rhel.7.x86_64.rpm).

Alternatively you could even run it with the link instead, skipping the need to download it first.

$ sudo dnf install https://github.com/PowerShell/PowerShell/releases/download/v<version>/powershell-<version>.rhel.7.<architecture>.rpm

To remove PowerShell, run the following.

$ sudo dnf remove powershell

### Install via container

#### Method 1: Podman container

Podman is an [Open Container Initiative](https://opencontainers.org/) (OCI) compliant drop-in replacement for Docker.

Microsoft provides a [PowerShell Docker container](https://hub.docker.com/_/microsoft-powershell). The following example will use that container with Podman.

For more information about Podman, visit [Podman.io](https://podman.io/). Fedora Magazine has a [tag](https://fedoramagazine.org/tag/podman/) dedicated to Podman.

To use PowerShell in Podman, run the following script:

$ podman run \ -it \ --privileged \ --rm \ --name powershell \ --env-host \ --net=host --pid=host --ipc=host \ --volume $HOME:$HOME \ --volume /:/var/host \ mcr.microsoft.com/powershell \ /usr/bin/pwsh -WorkingDirectory $(pwd)

This script creates a Podman container for PowerShell and immediately attaches to it. It also mounts the */home* and the host’s root directories into the container so they’re available there. However, the host’s root directory is available in */var/host*.

Unfortunately, you can only indirectly run host commands while inside the container. As a workaround, run *chroot /var/host* to chroot to the root and then run host commands.

To break the command down, everything is mandatory unless specified:

`-it`creates a persistent environment that does not kick you out when you enter it;`--privileged`gives extended privileges to the container (optional);`--rm`removes the container when you exit;`--name powershell`sets the name of the container to*powershell*;`--env-host`sets all host environment variables to the container’s variables (optional);`--volume $HOME:$HOME`mounts the user directory;`--volume /:/var/host`mounts the root directory to*/var/host*(optional);`--net=host --pid=host --ipc=host`runs the process in the host’s namespaces instead of a separate set of namespaces for the contained process;`docker.io/microsoft/powershell`enters the container;`/usr/bin/pwsh -WorkingDirectory $(pwd)`enters the container in the current directory (optional).

Optional but very convenient: alias *pwsh* with the script to easily access the Podman container by typing *pwsh*.

To remove the PowerShell image, run the following.

$ podman rmi mcr.microsoft.com/powershell

#### Method 2: Fedora Linux Toolbox container

Toolbox is an elegant solution to setup persistent environments without affecting the host system as a whole. It acts as a wrapper around Podman and takes care of supplying a lot of the flags demonstrated in the previous method. For this reason, Toolbox is a lot easier to use than Podman. It was designed to work for development and debugging. With Toolbox, you can run any command the same as you would directly on the Fedora Workstation host (including *dnf*).

The installation procedure is similar to the installation on the host methods, with the only difference being that those steps are done inside a container. Make sure you have the *toolbox* package installed.

Preparing and entering the Fedora 34 Toolbox container is a two step process:

- Creating the Fedora 34 Toolbox container
- Running the Fedora 34 Toolbox container

$ toolbox create --image registry.fedoraproject.org/f34/fedora-toolbox $ toolbox enter --container fedora-toolbox

Then, follow the instructions at [Method 1: Microsoft repositories](#method-1-microsoft-repositories).

Optional but very convenient: alias *pwsh* with *toolbox run –container fedora-toolbox* *pwsh* to easily access the Toolbox container by typing *pwsh*.

To remove the Toolbox container, make certain you have stopped the Toolbox session by entering *exit* and then run the following:

$ podman kill fedora-toolbox $ toolbox rm fedora-toolbox

## savemel117

I use powershell on my fedora from powershell core versione 6.0.

I manage for my job, two cluster of couchdb servers through PSCouchDB powershell module -> https://github.com/MatteoGuadrini/PSCouchDB on my fedora 34 with powershell 7.1.

Powershell is a beautiful script language because is Object Oriented Script Language

## Darvond

Okay, but why not script in python then?

To my understanding, python would accomplish this and more, without the cluttered syntax.

(And that’s ignoring whatever dogmatic issues people may have with Microsoft.)

## Brian

Good call on Python, and while it does beat PowerShell in terms of scripting, it doesn’t in terms of shell usage. So, if we are to achieve the similar functionality without PowerShell, we’d be learning and using two different languages. That is, for example, both Bash and Python.

## Andre Gompel

This is just ridiculous !

The Unix shell, bash being the most popular, are clean, effective, well documented, where the MS-windows Powershell is wordy, clumsy… and only designed to make up for the many weaknesses of Windows.

By buying and therefor controlling GitHub, MS once again tries to subvert free Open Source Linux. Stay way from this trap !

## silent-mobius

+1

## Czocher

FYI PowerShell has some merits. I agree it is clumsy and doesn’t fit the environment very well, but the advantage it has is it outputs objects instead of strings. This gives a very nice advantage in terms of operating on the return data at a later point. An alternative which fits better with the Linux environment is https://github.com/nushell/nushell . I suggest you have a look, it certainly is interesting.

## Arouene

Every programs and commands output strings in Linux. The objects that you can manipulates with PowerShell are only sugar coating translated from the string outputs, on a case by case basis. It means that you are on your own to use outputs that are not translated into objects, like with Get-Process, how do you filter process that are in “Uninterruptible sleep” ? Or others not so common commands ?

Besides PowerShell is heavy 100Mo per process, and slow.

For scripting it’s more usefull to use Python, that comes by default on nearly all Linux, and have the avantage to comes with billions libraries that can do anything, and is supported for nearly any ThirdParty services.

## Brian

Thanks for suggesting this. It really does seem quite interesting, and it’s even written in Rust!

## Stendall

+1

## Supernovae

There is nothing clumsy or subversive about powershell. It is a bit wordy, but who cares? It’s objects all the way down vs pipes and it’s open source with MIT license. Just like with bash/zsh you just build up aliases or tab completion and flexibility/efficiency comes with muscle memory. Because its objects, it’s pretty self explanatory. Powershell has very robust IDEs that simplify complex actions and for developers who live in .Net (which is open source too) they can use .net Objects from their shell. Pretty powerful. Cool to see this on linux.

## michael

sha256sum path/to/file

vs.

certUtil -hashfile path/to/file SHA256

choose your destiny

## Joao Rodrigues

String output is great because it’s simple and every other tool can do string input/output. It’s great that you can use tools like ps, awk, grep, find, etc. and pipe them together.

## Darvond

Okay, so can someone who can actually type lay explain the

actualadvantages and differences, rather than using a complicated example that seems to be purposefully convoluted and obfuscated?For example, the things I like about Bash are the command recall, the plaintext scripting, and the tab completion.

One also shouldn’t forget the elegance and origins of *sh’s syntax in the deeper Unix roots; there are videos in the AT&T Tech Archive even demonstrating this simplicity by showing the creation of pipelines into programs.

## Darvond

Can someone explain the more practical applications of Powershell in more lay terms?

Part of the elegance Unix shells is the simplicity in how piped programs can become programs themselves. This is even historically shown to be part of the reason for the existence of shell scripting being the way it is as shown as many historical archives.

Does Powershell have features comparable to the command recall, tab complete, and plaintext scripting?

## Strategist

My preference is always for bash – simple, clean, elegant and full control. From an Open Source perspective, you need to have full control of the input/process/output. It is the choice of the user to decide what to print. So some tool deciding what would be printable is like controlling your work. OOP better goes with programming and those languages. Things should be sleak and clean. The vast choice of tools is what makes linux and open source the best.

## Blackcrack

Microsoft should be banned from all Linuxdistros !

Reactos.org should more supportet to have a Clean under GPL2 WindowsNT

for free around the would and not depended at Microsoft !

best

Blacky

## Eonfge

You’re a total mad lad and I really appreciate that. Forget boring Bash or Zsh, use PowerShell! As for the cover image: A+ photoshopping skills… although I’m not sure if a gargantuan turtle blocking the road, is the kind of image that Microsoft was thinking of 😉

+1, will read the rest of the comments in joy

## TravelingBavarian

Great Article! Powershell is at least as good as bash or tcsh. And the comments saying “all other than us are evil” should grab their own nose, cause what you are doing is the same what you are accuse MS. For Example look how good VS Code is in a daily usage for an pro. Forget your stupid politics and religions.

This article is great and shows what can be done if you need it.

## Norbert J.

Thanks for this amazing article! I also have mixed feelings when Microsoft contributes to the open source ecosystem, but from a technical point of view it looks reasonable to do some things differently from 50 years ago when computers had very limited memory and processing power and were mostly used by specialists.

What about the legal situation? PowerShell is MIT licensed according to Wikipedia which means it could be provided as regular package in Fedora Linux and other distros?

## Darvond

Microsoft open sourcing a thing is always a little bit of a strange thing. You’ve got historical things like the https://github.com/Microsoft/winfile Winfile, https://github.com/microsoft/ms-dos MS-DOS, and my read is that when Microsoft makes something open source, they’re pretty much letting it out to pasture.

Which would mean the entire Windows Console Interface is out as far as they’re concerned.

But I’m purely speculating. I’m the kind of person who thinks Microsoft would drop the consumer end cold turkey if they could get away from that vast waste.

## michael

https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish

## Frederik

I am losing more and more respect for Red Hat and Fedora. After IBM bought Red Hat, the commitment to actual freedom is not just reduced, but almost entirely gone.

The example here is not an apples-to-apples comparison. Instead it is comparing a set of commands that are not part of the shell, that the article tries to compare power shell to, and how they are combined, to builtin functions in power shell.

At best this is naive and at worst, its a dishonest comparison. Writing functions that work, when combined, equivelantly to the built-in function in powershell is trivial, but will never be part of the shell. Instead it’s proper programmes that are easily combined, because they all have a shared interface.

The idea that you must know the output of ps is blatantly false and the entire premise for the comparison is broken. As the article points out, all you have to know, is that $2 exists and has the value you want. The same is true in the power shell example, where you must know that what WorkingSet is and how you can compare it to something.

## Richard England

I’m not able to see what impact using PowerShell on Fedora Linux has on your freedom or what the RedHat/IBM transaction had to do with the ability to use an MIT Licensed tool.

We all have opinions but I think it best to stick with the technology in this forum.

But thanks for being part of the conversation.

## Frederik

It’s not the first time that Fedora Magazine has advocated using software that originates in organizations that are fighting free software. If you think free software is about technology, I think you’re missing the entire point of technology.

## Richard England

There is no advocating for software in the Fedora Magazine. The articles are written by volunteers to provide information and introduce software and techniques that Fedora Linux users might find interesting and/or helpful.

This ends my involvement in this discussion.

## TheEvilSkeleton

Alright, let me be clear here.

Red Hat and IBM have nothing to do with this post. The authors of this article, Ozymandias42 and I, are not even affiliated with Red Hat or IBM; we never worked at Red Hat or IBM, we don’t pay for their services, etc. We felt like working together to write an article about PowerShell, so we did.

## Frederik

Nobody is saying you are. Nobody hinted or suggested that you are. These kinds of articles are just becoming more and more common on Fedora magazine. You write whatever article you feel like writing and publish them wherever you feel like publishing. The bulk of the comment was about how the comparison was dishonest, but for some reason that part has not been commented on at all.

## Brian

Your comment was that Red Hat and Fedora’s commitment to freedom was gone because Red Hat was purchased by IBM. You most CERTAINLY implied that Red Hat and IBM had something to do with this article. Otherwise, there would not be any point in mentioning it.

If you think an MIT-licensed shell is not free software, then I don’t know what to tell you.

## G Fernandes

If you really love objects all that much, simply script in Python.

## Daichi

On my intern days, I had just learned about Linux and Fedora (I was a Windows only user) and started writing Bash scripts to help managing the few Linux servers on the company I work for.

The number of Linux servers grew and I was already confident on Bash. Microsoft had just released the first versions of Powershell, but only later I found out how useful it was to automatize processes on Windows, much like bash was on Linux.

After getting used to it too, sometimes when I went back to Bash, I felt that the object manipulation was really handy in some occasions.

That’s when I started learning some Python.

Powershell has all the merits it deserve on Windows, maybe it came a bit late to Linux, but it’s good to know it’s supported on Fedora.

Maybe some idea for the next article be performance comparisons to Bash and Python?

## Matthew Darnell

PowerShell on linux? Sounds like you hit the proverbial jackpot with windows v linux. I enjoyed the article and this solves an issue for me. I’ve been interested in learning PowerShell but didn’t want to boot into windows to do it. Thank you

## Fenyx

Thanks for writing a nice introduction to Powershell. While it seems odd at first, it’s very powerful being able to pipe full objects rather than just text.

One small correction in the alias for pwsh you have “toolbox run –container fedora-toolbox pwsh” and it should be “toolbox run –container fedora-toolbox pwsh”. Probably caused by a text editor trying to help!

I look forward to more in this series.

## Stephen Snow

Powershell has it’s place on Windows where it is the only way to get some critical things accomplished, like rescuing a corrupted installation of Win10 for instance. I use it there quite often, and am thankful for MS dev’s, and users releasing some of their toolkit for general use. As for Linux, and my Fedora Linux Silverblue box, I don’t need Powershell, so I won’t be installing it, much like I won’t ever use their coding studio. That is more than a personal preference of software to use for my purposes, which due to the mere ubiquity, would go much smoother using closed source products, such as MS provides. It comes down to a personal choice to foregoe the ease of use trap, in favour of support for the Open Source alternatives already available either directly or indirectly. That is not to say the FOSS alternatives are less easy to use, just not as well known in most cases.

This openness of community that we are witnessing here, BTW is also why I read Fedora Magazine and have for a number of years. Ideas we talk about, even those we find controversial, can still be discussed openly.

So I, won’t be putting MS products onto my Fedora Linux box, but others are welcome to do so if that floats their boat’s (fit’s their needs). I won’t judge, but I will likely point out the FOSS alternatives, and why they are more suited to use.

Has anyone (else) bothered to notice the following (which I ripped from an Open Source Highlights article) “Linux inherited a lot from Unix, which has been around for a half-century. This means most of the tools you use in your Linux terminal probably either have a very long history or were written to emulate those historical commands. It’s a point of pride in the POSIX world that tools don’t need constant reinvention. In fact, there’s a subset of Linux users today who could run a version of Linux from before they were born without having to learn anything new. It’s tried, true, and reliable.” The full article can be read at https://opensource.com/article/21/7/rust-tools-linux?utm_medium=Email&utm_campaign=weekly&sc_cid=7013a000002wQ7rAAE