This books is to cover using Windows PowerShell, a .NET-based object-oriented command shell designed to replace batch scripting, Visual Basic Script and other scripting technologies on Windows. Later PowerShell versions are also available on Linux.

PowerShell is a modern Windows scripting technology based on .NET that features a full-blown programming language with access to .NET classes and with pipelines that are based on objects rather than character streams. It is a successor to scripting via cmd.exe and VBScript in Windows Script Host.

Availability edit

PowerShell 2.0 is part of Windows 7 and Windows Server 2008 R2.

PowerShell 5.1 was released along with the Windows 10 Anniversary Update.

PowerShell 7.2 is the most recent version, as of June 2022.

You can try PowerShell online in Tio (Try it online), in Linux environment.

Links:

Getting started edit

To get started, you can learn about the list of available cmdlets and their aliases, and learn about how to get help on them. Press Windows + R, enter "powershell", and the PowerShell console starts, into which you can then enter the oneliner examples below.

Examples for on getting help:

  • help
    • Outputs a help about the help system.
  • help *
    • Outputs list of items: aliases, cmdlets, help topics and other help items.
  • help d
    • Outputs list of aliases, cmdlets and help topics that start with d or that contain a word starting with d.
  • help d*
    • Outputs list of aliases, cmdlets and help topics that start with d.
  • help dir
    • Outputs help on the cmdlet that has alias "dir".
  • help dir -full
    • Outputs full help on the cmdlet that has alias dir.
  • help Get-ChildItem
    • Outputs help on the cmdlet.
  • help about
    • Outputs list of topics for help, that is, help items that start with "about".
  • help about_Arithmetic_Operators
    • Outputs help about the topic of arithmetic operators.
  • help about*Ari*
    • Outputs list of help topics that contain Ari as a substring.
  • help *Arith*
    • Outputs help about the topic of arithmetic operators as long as it is the only help item matching the pattern.

More examples:

  • $HOME
    • Outputs the value of variable HOME.
  • Variable
    • Outputs the variables.
  • gci env:os
    • Outputs the environment variable os, which is not among the variables per default.
  • $PSVersionTable.PSVersion
    • Outputs PowerShell version.
  • "Hello".GetType()
    • Outputs the type of the value. Keywords: typeof. GetType() is a method of the string object. Enables reflection.
  • (7).GetType(); 1.7.GetType(); (1, 2).GetType(); @{"Key" = "Value"}.GetType()
    • Outputs more types of .
  • "Hello".gettype()
    • Function names are case-insensitive, just like cmdlet names.
  • Get-ChildItem | Where {! $_.PSIsContainer} | Sort-Object -Property Length -Descending | Select-Object -Property Length,Name
    • Outputs files in the current folder sorted by size in descending order, outputting file length and file name only. Is an example of an object pipeline successively modifying the stream of object. Note that Select-Object -Property Length,Name does not actually select objects but rather constructs new objects that have only the selected properties from the original objects.
  • Get-ChildItem | Get-Member
    • Outputs object members including properties and methods. Enables reflection.
  • 4, 1.5 | Get-Member
    • Outputs object members for the objects types in the list, System.Int32 and System.Double.
  • [uint32]4 |Get-Member
    • Casts the integer literal to UInt32 type before getting the object members.
  • (Get-Content MyFile.txt).Length
    • Outputs the line count of a file. Search terms: "wc -l", the Unix analogue. An abbreviation is "(gc _test.txt).Length".
  • Get-Content myfile.txt | Measure-Object -Word
    • Outputs the word count of a file.

Links:

Online documentation edit

Online PowerShell documentation is available on a per-version basis.

Links:

Version edit

Finding the version of PowerShell engine:

  • $PSVersionTable

Links:

Arithmetic edit

PowerShell supports arithmetic based on what .NET supports.

Examples:

  • (1 + 2) * 3
    • A simple integer calculation.
  • (1 + 2) * 3 / 7
    • Floating point results are supported.
  • $a = 3/2; $a + 1
    • Intermediate results can be stored in a variable.
  • [Math]::Pow(2, 100); [bigint]::Pow(2, 100)
    • Exponentiation is available via Math and bigint; Math turns to floats for large numbers. Other usual floating-point functions are available, such as Sin, Cos, Sqrt, etc.
  • 0xFE -band -bnot 0xF
    • Bitwise arithmetic is supported. Further operators are -bor, -bxor, -shl and -shr.

Links

Get-Alias edit

Outputs current aliases. Alias: gal.

Examples:

  • gal
    • Lists all aliases.
  • gal g*
    • Lists all aliases that start with g.

Links:

Get-Help edit

Outputs help on a help item such as a cmdlet or topic. Its alias is help. Help for items can be downloaded using Update-Help cmdlet.

Examples:

  • Get-Help
    • Outputs a help about the help system.
  • Get-Help *
    • Outputs list of items: aliases, cmdlets, help topics and other help items.
  • Get-Help Get-ChildItem
    • Gets help on Get-ChildItem cmdlet.
  • Get-Help Get-ChildItem -full
  • help dir -full
    • As above, but relies on help being an alias for Get-Help and dir being an alias for Get-ChildItem.
  • help *
    • Outputs list of alisases, cmdlets, help topics and other help items.
  • help d
    • Outputs list of alisases, cmdlets and help topics that start with d or that contain a word starting with d.
  • help dir
    • Outputs help on the cmdlet that has alias "dir".
  • help dir -full
    • Outputs full help on the cmdlet that has alias dir.
  • help Get-ChildItem
    • Outputs help on the cmdlet.
  • help about
    • Outputs list of topics for help.
  • help about_Arithmetic_Operators
    • Outputs help about the topic of arithmetic operators.
  • help about*Ari*
    • Outputs list of help topics that contain Ari as a substring.
  • help *Arith*
    • Outputs help about the topic of arithmetic operators as long as it is the only help item matching the pattern.

Links:

Select-String edit

Finds string patterns in input, acting as a filter on lines; related to grep.

Examples:

  • "Cat", "Cat2", "Dog" | Select-String -Pattern "Cat"

Links:

Update-Help edit

Downloads a help item or a set of help items to the computer. Requires administrator privileges.

Examples:

  • Update-Help
  • Update-Help -Module Microsoft.PowerShell.Core

Links:

External links edit