Skip to content

powershell

PowerShell is a shell for Windows operating systems, and it was ported to Linux in 2016.

https://github.com/PowerShell/PowerShell/

Profile.ps1

On startup, powershell will run any .ps1 files it finds in the WindowsPowerShell directory under my documents. There is allegedly a Profile.ps1 file in there by default.

$env:Path = "c:\Users\user1\Dropbox\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\"

Examples

Restart a remote computer

Restart-Computer remotehostname -Force

Find a command that matches a substring

get-command *time*

Get help on commands that match a substring

get-help *time*

Show ACLs of the current dir

get-acl | format-list

Show system BIOS information

Get-WmiObject -ComputerName hostname win32_bios

Show object methods

$foo | Get-Member

Browse the registry

Set-Location HKCU:\Software\Microsoft\Windows\
Get-ChildItem

Show top processes

while (1) { ps | sort -desc cpu | select -first 30; sleep -seconds 1; cls }

Browse the Cert store

Set-Location cert:\CurrentUser\
Get-ChildItem

Get a list of stopped services

Get-Service | Where-Object { $_.Status -eq "Stopped" }

Compare two objects

This will only show the lines that are not common:

Compare-Object $(Get-VIPrivilege -role admin) $(Get-VIPrivilege -role member)

Save object to a csv

Get-Process | Export-Csv -Encoding unicode processes.csv

Load object from a csv and parse it

Import-Csv ./processes.csv | Where-Object { $_.Name -like "*systemd*" } | Select-Object -last 10 | Format-Table

Replacement for unix tail

tail filename

Get-Content [filename] | Select-Object -Last 10

tail -f

Get-Content -Path "C:\scripts\test.txt" -Wait

Replacement for unix wc

Get-Content test.csv | Measure-Object -line -word -character

Replacement for unix time

Measure-Command { Sleep 5 }

Replacement for unix grep -B2 -A1

Get-Content test.csv | Select-String "searchstring" -Context 2,1 -CaseSensitive

Install PowerShell in Ubuntu 18.04

wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb && \
    dpkg -i packages-microsoft-prod.deb && \
    apt-get update && \
    apt-get install -y powershell

See Also