PowerShell Memos

This post contains some of my various and continuous memos for exploring and learning PowerShell. These are not organized in any particular order.

Get-Alias (no explanation needed)

Get-Command: Use when unsure which commands are available

Get-Member: Shows the methods and properties of an object. Also shows the object type. Example:

PS C:\> $name = "Markku"
PS C:\> $name | Get-Member

   TypeName: System.String

Name           MemberType        Definition
----           ----------        ----------
Clone          Method            System.Object Clone(), System.Object ICloneable.Clone()
CompareTo      Method            int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Objec...
Contains       Method            bool Contains(string value)
CopyTo         Method            void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EndsWith       Method            bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool...

ForEach-Object: Use for looping. Example:

PS C:\> $aliases = Get-Alias
PS C:\> $aliases.Length
PS C:\> $aliases | ForEach-Object {$_.Name} | Select-Object -First 5
PS C:\>

There was also Select-Object that is usually used for selecting specific properties but is also used for the “First/Last x” purposes:

PS C:\> Get-Service | Select-Object -Property Name, Status -First 5

Name             Status
----             ------
AarSvc_f74d7    Stopped
AdobeARMservice Running
AESMService     Running
AJRouter        Stopped
ALG             Stopped

PS C:\>

Select-Object also has -ExcludeProperty option that is useful for example when outputting to JSON with ConvertTo-Json and you want to skip some properties for data size reasons (Select-Object -ExcludeProperty cim* | ConvertTo-Json).

Where-Object is used for selecting/filtering specific objects:

PS C:\> $aliases | Where-Object {$_.name -eq "ls"}

CommandType     Name                         Version    Source
-----------     ----                         -------    ------
Alias           ls -> Get-ChildItem

PS C:\>

Lists are called arrays in PowerShell, and they can be created like this:

PS C:\> $list = 1, 2, 3
PS C:\> $list
PS C:\>

More advanced way is to use @() and statements in it, like $list = @(1; 2; 3) (these statements just produce their values).

Python dictionaries are hash tables in PowerShell:

PS C:\> $hashtest = @{One=1; Two=2; Three=3}
PS C:\> $hashtest

Name                           Value
----                           -----
One                            1
Three                          3
Two                            2

PS C:\> $hashtest["One"]
PS C:\>

PowerShell variable names are sometimes prefixed with something like “script:” or “global:“, that’s scoping.

Write-Host outputs data to the console, while Write-Output outputs data to the PowerShell pipeline so that you can process it further. For example:

PS C:\> Write-Host "Markku" | Out-Null
PS C:\> Write-Output "Markku" | Out-Null
PS C:\>

Updated: June 26, 2021 — 20:00

Leave a Reply