14 Aug 2022
PowerShell Core, introduced in 2016 is open-source and designed to run cross-platform on Windows, Mac, and Linux. PowerShell Core runs on top of .NET Core, a cross-platform, open-source version of .NET.
PowerShell and PowerShell Core can run in parallel, and its important to remember that not all cmdlets and features are available in PowerShell Core.
The following are my notes while taking part in LinkedIn Learning course Learning PowerShell Core.
Windows Installation Documentation
Download Windows (x64) LTS MSI file - at the time, version 7.2 LTS is the current stable.
The default install options are
Once installed you can verify the installed version
$PSVersionTable
To call PowerShell Core from a normal command prompt use the command
PWSH
Ubuntu Install Script
# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
Debian 11 Install Script
# Install system components
sudo apt update && sudo apt install -y curl gnupg apt-transport-https
# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
# Register the Microsoft Product feed
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list'
# Install PowerShell
sudo apt update && sudo apt install -y powershell
Run and Verify
PWSH
$PSVersionTable
To access help by using the command below or help
.
Get-Help
To update your offline help files, use the update command.
Update-Help
Use the online help, through a browser
Get-Help -Name Get-Cotent -Online
Help has different levels of detail, starting from the basic to full.
Get-Help -Name Get-Cotent
Get-Help -Name Get-Cotent -Detailed
Get-Help -Name Get-Cotent -Full
Get-Help -Name Get-Cotent -Examples
The help documentation is always a good place to start when learning what a command does.
Get-Help Get-Command
List all commands available
Get-Command
Get all commands that start with the verb “Get”.
Get-Command -Verb Get
Get all commands that contain the noun “Module”, this can also be wrapped with wildcards to provide more results.
Get-Command -Noun Module
Get commands with a specific parameter, such as “ComputerName”.
Get-Command -ParameterName ComputerName
Get all the commands contained within a specific module, in the example below I use the “PackageManagement” module.
Get-Command -Module PackageManagement
To list all configured alias, use the following command;
Get-Alias
If you wanted to know a specific alias, just append that to the end
Get-Alias -Name cls
The pipeline allows the administrator to stitch one command to another, passing the object through, for example.
Get-Service -Name "BITS" | Start-Service
To see all the available methods and properties on an object, use Get-Member
.
Get-Date | Get-Member
PowerShell hides some properties, these can all be shown by using the Select-Object
command.
Get-Date | Select-Object *
To find all the currently imported modules
Get-Module
The above command does not show all available modules, to list all available modules use the following command:
Get-Module -ListAvailable
To list the available commands within a module use:
Get-Command -Module PackageManagement
If you wish to remove a module from current PowerShell session, you can use the command:
Remove-Module -Name PackageManagement
Modules are stored in the Module Path
$env:PSModulePath
To import a module
Import-Module -Name PackageManagement
To import a module from a different location
Import-Module -Name /home/user/Downloads/module
Additional modules can be found on PowerShell Gallery, the web interface is easy to use.
To find modules via PowerShell use
Find-Module
Most modules are designed for Windows PowerShell, but we can filter by tag for PowerShell Core
Find-Module -Tag 'PSEdition_Core'
To install a module either use a pipe or install command
Find-Module -Name AdminToolbox.ActiveDirectory | Install-Module
Install-Module -Name AdminToolbox.ActiveDirectory
Its important to keep modules up-to-date, review your versions
Get-Module -Name AdminToolbox.Remoting -ListAvailable
Find-Module -Name AdminToolbox.Remoting
Update by using the command
Update-Module -Name AdminToolbox.Remoting
To uninstall a module completely
Uninstall-Module -Name AdminToolbox.ActiveDirectory
Are blocks of code that can be reused, if you PowerShell scripts contain repeated code, this is a good opportunity to utilise a function.
Functions should be defined before they are needed, at the top of the script.
When naming a function always try to use the PowerShell verb noun convention.
Parameters provide the mechanism that allows a cmdlet to accept input
function Test-ServiceStarted {
param(
[Parameter()]
[string]$Name
)
if ((Get-Service -Name $Name).Status -eq 'Running'){
$True
} else {
$False
}
}
Test-ServiceStarted -Name wuauserv
Any Windows Server after 2012 R2 will have PSRemoting enabled by default, but there are additional configuration required such as; Firewall exeception and the Service running.
To enable use the command
Enable-PSRemoting
Create a PowerShell Temporary session
$credential = Get-Credential
Invoke-Command -ComputerName 192.168.100.169 -Credential $credential -ScriptBlock { hostname }
Create a PowerShell session
$credential = Get-Credential
Enter-PSSession -ComputerName 192.168.100.169 -Credential $credential
SSH is used to connect to and from Linux systems.
Install OpenSSH server
Get-WindowsCapability -Online | Where-Object Name -Like 'OpenSSH.Server' | Add-WindowsCapability -Online
Configure service
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
Edit file C:\ProgramData\ssh\sshd_config
, adding the following Subsystem
Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -NoLogo
Ensure PasswordAuthentication
is configured to yes
.
Restart the sshd
service
Restart-Service sshd