PowerShell

Version

Windows Server 2012 ships with PowerShell version 3

To check version use...

$PSVersionTable.PSVersion

Running PowerShell scripts

Assuming you are in a PowerShell window and the script you want to run has a .ps1 extension and is in your current working directory, run...

./myscript.ps1

From outside PowerShell (i.e. Windows command prompt), you can use...

powershell -File ./myscript.ps1

If your default script execution policy does not allow this, try...

powershell -executionpolicy bypass -File ./myscript.ps1

Current Environment

Get-Childitem -Path Env:* | Sort-Object Name

${env:UserName}

${env:UserDomain}

${env:ComputerName}

${env:Path}

Get-Childitem $Env:Temp -Recurse

Help About_Environment_Variables

Get-Module -ListAvailable

All current environment variables

Current User

Current User's Domain

Current Computer Name

Path

Show Temp Files

Get more information

List installed PowerShell modules

Windows Version/Build information...

Get-WmiObject Win32_OperatingSystem | Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber | FL

Examples

powershell "dir | tee test.txt" 

Remote Commands

Interactive Session...

Enter-PSSession myServer

...

Exit-PSSession

Remote Command...

Invoke-Command -ComputerName myServer, myOtherServer -ScriptBlock {Get-UICulture}

To run a script (available on your local computer)...

Invoke-Command -ComputerName myServer, myOtherServer -FilePath c:\mypath\myScript.ps1

Further information...

https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7

Examples

Windows Version for Remote Computer...

Get-WmiObject Win32_OperatingSystem -ComputerName "Remote_Machine_Name" |

Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber | FL

Adding cmdlets

PowerShellGet

PowerShellGet is already installed if you have... Windows 10 or newer, Windows Server 2016 or newer, Windows Management Framework (WMF) 5.0 or newer, or PowerShell 6. For Windows Server 2012 it is recommended to install the latest compatible version of PowerShell (at time of writing this was PowerShell 7)

Bibliography

https://dbatools.io/https://stackify.com/what-are-powershell-commands/https://devblogs.microsoft.com/scripting/table-of-basic-powershell-commands/https://ss64.com/ps/https://www.techrepublic.com/blog/10-things/10-powershell-commands-every-windows-admin-should-know/https://stackoverflow.com/questions/796476/displaying-windows-command-prompt-output-and-redirecting-it-to-a-filehttps://binarynature.blogspot.com/2010/04/powershell-version-of-df-command.htmlhttps://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7https://adamtheautomator.com/check-powershell-version/https://github.com/PowerShell/PowerShell/releases/tag/v7.0.3https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-7https://adamtheautomator.com/powershell-dns/https://learn.microsoft.com/en-us/powershell/scripting/install/powershell-support-lifecyclehttps://learn.microsoft.com/en-us/powershell/scripting/whats-new/cmdlet-versions
Powershell Scriptshttps://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/ise/how-to-write-and-run-scripts-in-the-windows-powershell-isehttps://www.windowscentral.com/how-create-and-run-your-first-powershell-script-file-windows-10https://www.tutorialspoint.com/how-to-run-a-powershell-script-from-the-command-prompthttps://stackoverflow.com/questions/2035193/how-to-run-a-powershell-script
Visual Studio Codehttps://code.visualstudio.com/docs/
PowerShell to EXEhttps://stackoverflow.com/questions/48338017/convert-powershell-script-to-exe
Wordwraphttps://stackoverflow.com/questions/1059663/is-there-a-way-to-wordwrap-results-of-a-powershell-cmdlet
PowerShellGethttps://learn.microsoft.com/en-us/powershell/scripting/whats-new/cmdlet-versions#powershellget-2x
Current Environmenthttps://stackoverflow.com/questions/2085744/how-do-i-get-the-current-username-in-windows-powershellhttps://www.computerperformance.co.uk/powershell/environmental-variables/https://morgantechspace.com/2017/09/how-to-find-windows-os-version-using-powershell.html
Driveshttps://www.computerperformance.co.uk/powershell/get-psdrive/https://www.computerperformance.co.uk/powershell/wmi-disk/
Splattinghttps://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting
WMI/CIMhttps://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/07-working-with-wmihttps://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance
Win32https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk