lang/ps/ NotesLearnPowershellBook001


What follows is mostly command examples to illustrate things. Additional notes will be added sparingly.

There is the PowershellISE.

Powershell is CaseInsensitive but commonly things are in TitleCase like e.g. Get-SmbShare

Dir; ls; copy; cd; move; ren; mkdir; rmdir; del
type # cat contents of file
Dir | more
dir > file.txt

cd hkcu: # gets to HKEY_CURRENT_USER in registry
new-psdrive -name flibble -psprovider filesystem -root c:\users
new-psdrive -name hexvision -psprovider filesystem -root \\mymachine\myshare\subdir

net use # can use external programs as expected

$files = Get-ChildItem -Path C:\Windows -Filter "*.exe"
$files # will dump variable to terminal
$env:Path # environment variables

Get-ChildItem # ls
Set-Location # cd
Move-Item # mv
Rename-Item # mv
Remove-Item # rm
Copy-Item # cp
Get-Content # cat
New-Item # Mkdir
Get-ChildItem -recurse

Get-Service
Get-Process
Get-EventLog
New-Service

Help

Get-Help Get-ChildItem -Online
Get-Help New-SmbShare | More
Get-Help *Move*
Get-Help Get-EventLogd

Pipelines

Powershell sends objects down pipelines whereas bash sends text/binary down pipelines. Both approaches have advantages and disadvantages.

Get-Process | Export-CSV procs.csv
Compare-Object # diff
Get-Process | Export-CliXML

# diff
Get-Process | Export-CliXML reference.xml
Diff -reference (Import CliXML) -difference (Get-Process) -property Name

Dir | Out-File DirectoryList.txt

Dir # is the same as
Dir | Out-Default

Get-Service | ConvertTo-HTML
Get-Service | ConvertTo-HTML | Out-File services.html

Get-Process | Stop-Process # DON'T DO THIS
Get-Process -name Notepad | Stop-Process # Stop all notepads