Search notes:

PowerShell

powershell.exe is basically an interpreter for the .NET assembly System.Management.Automation.
This assembly is loaded by powershell.exe.

Environment variables

PowerShell accesses the values of environment variables by prefixing their name with $env: (technically: $env is a PowerShell drive).
PS> echo $env:userprofile
The values of environment variables stored in the registry under HKEY_CURRENT_USER\Environment and HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment can be read and written like so:
PS C:\> echo [environment]::getEnvironmentVariable("PATH", "user"    )
PS C:\> echo [environment]::getEnvironmentVariable("PATH", "machine" )
PS C:\> [environment]::setEnvironmentVariable("A_VAR"      , "a value"      , "user") 
PS C:\> [environment]::setEnvironmentVariable("ANOTHER_VAR", "another value", "machine") 
It's noteworthy that with this construct, it's not necessary to send a WM_SETTINGCHANGE after changing or creating a variable because PowerShell is smart enough to do that without being asked.

Tilde

Apparently, the PowerShell, in the spirit of a Unix shell, expands the tilde (~) to the %UserProfile% directory:
PS  C:\> dir ~
However, it seems that it stumbles if a variable contains a tilde …

Show version

The version of the PowerShell installation is revealed in the automatic variable $psVersionTable.

(Some interesting) command line options/parameters

-file -f Specify a script to be run by the Powershell interpreter. Value can be - which reads the «script» from stdin. Possibly to be combined with -noExit.
-command -c Specify a command to be executed, then exit (unless -noExit is specified). Value can be a string, a script block or -. (See also Executing a PowerShell script in a cmd.exe batch file)
-configurationName -config Specify configuration endpoint in which PowerShell is run.
-customPipeName Name for an additional IPC server (named pipe), used for debugging or other cross-process communication.
-encodedCommand -e, -ec Command is Base64 encoded.
-executionPolicy -ex, -ep Sets the execution policy (which is stored in the environment variable $env:psExecutionPolicyPreference.)
-inputFormat -inp, -if Description of data format that is sent to PowerShell. Possible values: text or xml (which is serliazed CLIXML format).
-interactive -i Opens an interactive PowerShell session. (See also -nonInteractive)
-login -l Starts PowerShell as a login shell using /bin/sh. Only useful on Linux and macOS. Must be first parameter when used.
-MTA, -STA Start the shell using a multi-threaded/single-threaded apartment. Windows only. Defaults to -STA if not specified
-noExit -noe Do not exit after running startup commands.
-noLogo -nol Do not show copyright banner at startup. Without -noLogo, PowerShell displays a copyright banner and, if executing the profile takes longer than 500 ms(?), it also prints Loading personal and system profiles took xyz ms.
-nonInteractive -noni Non-interactive prompt
-noProfile -nop Do not load the PowerShell profile. (see the automatic variable $profile).
-outputFormat -o, -of Specifies output format. Possible valueare text and XML (serialized CLIXML format).
-settingsFile -settings Overrides system-wide powershell.config.json file (which, by default, is tored in the directory that is pointed at by $psHome)
-version -v Show used version of PowerShell. See also the automatic variable $psVersionTable.
-windowStyle -w Window style for the session. Possible values are normal, minimized, maximized and hidden.
-workingDirectory -wd Sets the initial working directory by executing set-location -literalPath …. (This parameter Seems to be available in PowerShell Core only)
-help -?, /? Show help

Default option

If PowerShell is invoked with no options (but values are present in the command line), the default options for PowerShell 5.1 (powershell.exe) is -file and -command for Powershell 7 (pwsh.exe).
This change was necessary when PowerShell was ported to Unix like environments in order to support shebang lines.

-command (or -c) to execute command(s)

-c statement(s) creates a new PowerShell process in which one or more PowerShell statement(s) are executed.
The value for -c can be:
powershell -c 'write-host going to loop; foreach ($i in 1..10) { $i }; write-host loop is finished'

-noExit

When using -c, the new PowerShell session is terminated when it has finished evaluating the statements. This termination can be prevented by using the -noExit parameter instead of the -c parameter:
PS C:\> $PID 
95420
PS C:\> pwsh -c '$PID'
87390
PS C:\> $PID
95420
PS C:\> pwsh -noExit '$PID'
14464
PS C:\> $PID
14464
PS C:\> exit
PS C:\> $PID
95420

-wd (initial working directory)

-wd sets the initial working directory.

-noLogo (hide startup banner)

-noLogo hides the startup banner.

Specifying boolean values

Switchable options can be given a boolean value with the following syntax:
pwsh … {-all:$false}

32-bit vs 64-bit executable

On a 64-bit Windows installation, the 32-bit executable of Powershell is located under C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe while the 64-bit executable is found at C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
In a running PowerShell session, the bitness of the running PowerShell host can be determined with
PS C:\> $([System.Runtime.InterOpServices.Marshal]::SizeOf([System.IntPtr]::Zero)*8)
I was forced to use the 32-bit executable when I used PowerShell with ADO DB to interact with MS Access.
This simple cmd.exe batch file starts the 32-bit executable of PowerShell.

Nice features

Create a file

The following line creates a text file whose first (and only) line reads Hello world.:
PS C:\> ${c:\users\Rene\tq84.txt}="Hello World"

adminsitrative constants

1kb
2mb
3gb
4tb
5pb
The adminsitrative constants are not consistent with the IEC recommendations (kibibite etc…), 1kb evaluates to 1024, 1mb to 1048576 etc.

Starting PowerShell

PowerShell can be started

PowerShell Core and PowerShell 7

PowerShell Core seems to be PowerShell 6.x.
PowerShell Core does not intend to replace or upgrade the «regular» PowerShell, rather, it tries to solve different tasks.
PowerShell 7.x dropped the Core from its name (but it is still displayed in psVersionTable.psEdition).
The System.Management.Automation.ClrFacade class contains all diverging code for «FullCLR» and «CoreCLR».
PowerShell 7.x can be installed with winget.exe:
C:\> winget search PowerShell
…
C:\> winget install --id Microsoft.PowerShell --source winget
Note, when I tried to install PowerShell with the option --scope user, winget reported it had found a version that is not the most recent one.

Misc

The ampersand (&) is the call operator.

TODO

What is the significance of the environment variable __PSLockdownPolicy and its relationship to wldp.dll.
%APPDATA%\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt
The equivalent of seems to be set-strictMode -version 2.
help about_common*, -confirm, -whatIf.
.psc files are PowerShell console files. They specify one or more PSSnapIns to load into memory at startup.
[object], [psobject], [pscustomobject]
The prompt and tabCompletion functions. BTW: is tabCompletion replaced by psReadLine?

Line continuation

A command can be spread over multiple lines by ending a line with a space followed by a grave accent (aka backtick)
write-host `
foo `
bar `
baz 
Compare with the caret in cmd.exe.

Command types

Command types are
These correspond to the possible values for the -commandType parameter of the get-command cmdlet.

The tilde in short file names

Windows has a concept of (DOS) short file names where an arbitrary long filename is shortened to eight characters (without suffix). It uses a tilde for that. Unfortunately, in PowerShell, the tilde has a special meaning (home directory). So, if an environment variable contains a tilde, it is wrongly recognized in PowerShell.

Docker image

$ docker pull mcr.microsoft.com/powershell
…
mcr.microsoft.com/powershell:latest
$ docker run -it --name pwsh mcr.microsoft.com/powershell:latest
PowerShell 7.4.2

PS /> which pwsh
/opt/microsoft/powershell/7/pwsh

PS /> grep PRETTY_NAME /etc/os-release
PRETTY_NAME="Ubuntu 22.04.4 LTS"

PS /> exit
$ docker start -i pwsh
 A new PowerShell stable release is available: v7.4.5
   Upgrade now, or check out the release page at:
     https://aka.ms/PowerShell-Release?tag=v7.4.5
Trying to update Powershell to the latest version
PS /> invoke-restMethod  https://aka.ms/install-powershell.ps1 -outFile $env:temp/install-powershell.ps1
PS /> & $env:temp/install-powershell.ps1
…
PowerShell has been installed at /root/.powershell

PS /> /root/.powershell/pwsh
PowerShell 7.4.5

PS /> mv /opt/microsoft/powershell/7/ /opt/microsoft/powershell/7.bak
PS /> mv /root/.powershell/ /opt/microsoft/powershell/7
PS /> exit
Updating PowerShell seems to be successful (but is this the way it is supposed to be done?)
$ docker start -i pwsh
PowerShell 7.4.5
Trying to set hostname
PS /> hostnamectl set-hostname pwsh-host
hostnamectl: The term 'hostnamectl' is not recognized as a name of a cmdlet, function, script file, or executable program

PS /> hostname pwsh-host
hostname: you must be root to change the host name
… but I am root:
PS /> id
uid=0(root) gid=0(root) groups=0(root)
It seems the following does not have the desired effect:
PS /> echo pwsh-host > /etc/hostname
Install Git:
PS /> git
git: The term 'git' is not recognized as a name of a cmdlet, function, script file, or executable program.
…
PS /> apt update
PS /> apt install -y git
Add myself as user:
PS /> useradd -s $(which pwsh) -m rene
PS /> chpasswd
rene:***
^D
Install sudo:
PS /> apt install -y sudo
PS /> usermod -aG sudo rene
PS /> exit
$ docker start pwsh
pwsh
$ docker exec --user rene -it pwsh /opt/microsoft/powershell/7/pwsh
TODO:
  • Why am I not in my home directory after doing docker exec --user rene -it pwsh /opt/microsoft/powershell/7/pwsh

See also

Examples
Pipelines
When a PowerShell process is started, it spawns conhost.exe as child process (which is the console-host and controls the appearance and functionality of PowerShell).
Script cannot be loaded because running scripts is disabled on this system
Unix and cmd.exe like default aliases
PowerShell: language
Configuring the options of MS-Word with PowerShell.
The .NET class System.Management.Automation.PowerShell
Windows 10 S does not have a PowerShell.
PowerShell integrates with Antimalware Scan Interface (AMSI).
Powershell command noun: eventLog
The Microsoft.PowerShell namespace

Links

about powershell @ github

Index