TExcelTAD
Home › PowerShell › PowerShell on Windows

PowerShell on Windows

PowerShell was born as a Windows administration tool, and many of its most famous commands manage Windows itself: services, processes, the event log, WMI/CIM, scheduled tasks and Active Directory. PowerShell 7 is cross-platform, but these particular commands only exist on Windows, because the systems they manage only exist there.

This lesson shows the most important ones in non-runnable blocks with sample output from a Windows machine, and runnable examples of how to write scripts that behave well on every OS.

Detecting the platform

powershell · try it yourself
$platform = if ($IsWindows) { 'Windows' } elseif ($IsMacOS) { 'macOS' } else { 'Linux' }
"Running on $platform"

foreach ($cmd in 'Get-Service', 'Get-CimInstance', 'Get-EventLog', 'Get-ChildItem') {
    $exists = [bool](Get-Command $cmd -ErrorAction SilentlyContinue)
    '{0,-16} available: {1}' -f $cmd, $exists
}
Running on Linux
Get-Service      available: False
Get-CimInstance  available: False
Get-EventLog     available: False
Get-ChildItem    available: True

On Linux, the Windows-only cmdlets simply don't exist. Check with Get-Command (or $IsWindows) before using them in scripts meant for several platforms.

Services

text
PS C:\> Get-Service -Name Spooler, W32Time

Status   Name               DisplayName
------   ----               -----------
Running  Spooler            Print Spooler
Stopped  W32Time            Windows Time

PS C:\> Get-Service | Where-Object { $_.Status -eq 'Stopped' -and $_.StartType -eq 'Automatic' }
PS C:\> Restart-Service -Name Spooler -WhatIf
What if: Performing the operation "Restart-Service" on target "Print Spooler (Spooler)".
PS C:\> Set-Service -Name W32Time -StartupType Automatic     # needs an elevated session

On Linux, services are managed with systemctl (a native program, so its output is text): systemctl is-active nginx.

Processes

Get-Process works on every platform, but its values (IDs, memory, CPU time) change from second to second, so here's a sample:

text
PS> Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 3 Name, Id,
>>     @{ N = 'MemMB'; E = { [math]::Round($_.WorkingSet64 / 1MB) } }

Name      Id MemMB
----      -- -----
chrome  9120   612
Code    4410   498
pwsh    7788   143

PS> Stop-Process -Name notepad -WhatIf
What if: Performing the operation "Stop-Process" on target "notepad (5132)".
PS> Start-Process notepad.exe -ArgumentList 'C:\Temp\notes.txt'

WMI / CIM: asking Windows about itself

CIM (the modern face of WMI) exposes hardware and OS information as classes you can query. Use the *-CimInstance cmdlets; the old Get-WmiObject was removed in PowerShell 7:

text
PS C:\> Get-CimInstance -ClassName Win32_OperatingSystem |
>>     Select-Object Caption, Version, LastBootUpTime

Caption                    Version     LastBootUpTime
-------                    -------     --------------
Microsoft Windows 11 Pro   10.0.26100  10/14/2026 7:52:11 AM

PS C:\> Get-CimInstance Win32_LogicalDisk -Filter "DriveType = 3" |
>>     Select-Object DeviceID, @{ N = 'FreeGB'; E = { [math]::Round($_.FreeSpace / 1GB, 1) } }

DeviceID FreeGB
-------- ------
C:        112.4
D:        734.0

PS C:\> Get-CimInstance Win32_LogicalDisk -ComputerName srv01, srv02     # remote, over WinRM

Event log and scheduled tasks

text
PS C:\> Get-WinEvent -LogName System -MaxEvents 2 | Select-Object TimeCreated, Id, LevelDisplayName

TimeCreated            Id LevelDisplayName
-----------            -- ----------------
10/17/2026 9:14:03 AM 7036 Information
10/17/2026 9:12:47 AM   16 Information

PS C:\> $action  = New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument '-File C:\Scripts\cleanup.ps1'
PS C:\> $trigger = New-ScheduledTaskTrigger -Daily -At 3am
PS C:\> Register-ScheduledTask -TaskName 'Nightly cleanup' -Action $action -Trigger $trigger

On Linux, the equivalent of a scheduled task is a cron entry or a systemd timer that runs pwsh -File /opt/scripts/cleanup.ps1.

Active Directory

The ActiveDirectory module (part of RSAT on Windows, or a domain controller) manages users, groups and computers:

text
PS C:\> Import-Module ActiveDirectory
PS C:\> Get-ADUser -Filter "Department -eq 'Research'" -Properties Title | Select-Object Name, Title

Name           Title
----           -----
Ada Lovelace   Lead Analyst
Alan Turing    Researcher

PS C:\> New-ADUser -Name 'Grace Hopper' -SamAccountName ghopper -Path 'OU=Staff,DC=contoso,DC=com'
PS C:\> Add-ADGroupMember -Identity 'Research' -Members ghopper

Windows paths and elevation

Topic Windows Cross-platform tip
Paths C:\Users\Ada\Documents Join-Path $HOME 'Documents'
Admin rights "Run as administrator" check before doing admin work
Line endings CRLF PowerShell reads both

Scripts that need admin rights should check for them first. This test is portable: it uses the Windows API on Windows and the user ID elsewhere:

powershell · try it yourself
function Test-Elevated {
    if ($IsWindows) {
        $id = [Security.Principal.WindowsIdentity]::GetCurrent()
        ([Security.Principal.WindowsPrincipal]$id).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    } else {
        (id -u) -eq '0'
    }
}

$docs = Join-Path $HOME 'Documents'
"Documents folder: $docs"
"Elevated: $(Test-Elevated)"
Documents folder: /tmp/Documents
Elevated: True

The verification container runs as root, so it reports True; on a normal desktop session you'd see False.

Tip: Write the "work" of your scripts with cross-platform cmdlets and objects, and isolate Windows-only calls in small functions guarded by $IsWindows. Your automation then runs on Windows servers, Linux containers and macOS laptops alike.

Go further: PowerShell Security: Logging, AMSI, CLM and JEA covers script-block logging, AMSI, Constrained Language Mode and Just Enough Administration.

Test yourself

Which cmdlet replaces Get-WmiObject in PowerShell 7?

  1. Get-Wmi
  2. Get-CimInstance
  3. Get-ComputerInfo only
  4. Invoke-Wmi

Why does Get-Service fail on Linux with PowerShell 7?

  1. Linux has no services
  2. The cmdlet only exists on Windows, because it manages the Windows service controller
  3. It needs -Linux
  4. It needs sudo

What is the portable way to build a path to the user's Documents folder?

  1. 'C:\Users\' + $env:USERNAME + '\Documents'
  2. Join-Path $HOME 'Documents'
  3. '~/Documents' only
  4. $env:USERPROFILE + '/Documents'