Now that PowerShell is available on various Linux distributions (such as CentOS, Red Hat, and Ubuntu), it is natural for Windows sysadmins to begin tinkering on Linux machines. One of the first thoughts I had after hearing the news that PowerShell was cross-platform was “I wonder what I can script with PowerShell on Linux?” The answer to this question is: quite a lot! In this article I will show you two very simple PowerShell scripts you can use on CentOS.

PowerShell Core does not have the fancy cmdlets Windows users are familiar with, such as manipulating the firewall, services, or storage since these are native only to Windows. In fact, at the time of this article there are only 352 commands in PowerShell Core as Get-Command shows.

PS /root> Get-Command | Measure-Object | Select-Object -Property Count Count ----- 352 1 2 3 4 5 PS / root > Get - Command | Measure - Object | Select - Object - Property Count Count -- -- - 352

So how can we use PowerShell to create scripts on Linux like we are used to on Windows? We have to use native Linux commands like crontab, df, and systemctl that perform tasks for which PowerShell does not have cmdlets. Since PowerShell is just another shell on Linux, it can run these Linux commands within PowerShell.

Adding a PowerShell module in Linux

While you can create PowerShell functions in a .ps1 file on Linux and run them within the console, I prefer to create modules. Currently, PowerShell Core on CentOS has three directories in which to store modules:

While you can create PowerShell functions in a .ps1 file on Linux and run them within the console, I prefer to create modules. Currently, PowerShell Core on CentOS has three directories in which to store modules:

PS /root> ($env:PSModulePath).split(':') /root/.local/share/powershell/Modules /usr/local/share/powershell/Modules /opt/microsoft/powershell/6.0.0-beta.4/Modules 1 2 3 4 PS / root > ( $ env : PSModulePath ) . split ( ':' ) / root / . local / share / powershell / Modules / usr / local / share / powershell / Modules / opt / microsoft / powershell / 6.0.0 - beta . 4 / Modules

Read more at 4sysops.com