Bash equivalent in PowerShell
Variables
Check file existence
Test-Path "path\\to\\file.txt"Get file content
Get-Content “filename”
$content = Get-Content "C:\\myfolder\\myfile.txt"Read from bottom
$content = Get-Content "filename" -Tail 2 # last 2 lineRead specific line
$content = (Get-Content "filename")\[2\]Read range of lines
$content = (Get-Content "filename")\[2..5\]Search in the file content
$content | Where-Object { $_ -like "\*PATTERN\*" }
$content | Select-String -Pattern "\*PATTERN\*" # like grepWrite to file
Write-Output "some text" | Out-File -FilePath output.txt
Write-Output "some text" | Out-File -FilePath output.txt -AppendHandling CSV file
Read CSV file
$csv_content = Import-Csv "path\\to\\file.csv"
Import-Csv returns System.ArrayCreate CSV file
Add-Content -Path file.csv -Value "Name","Class","Percentage"Handling XML file
Read XML File
[xml]$content = Get-Content "file.xml"
$content.Gettype() # System.Xml.XmlNoteWrite XML File
Export-Clixml output.txtSelect-Object
Command | Select-Object -First 5
Command | Select-Object -Last 5Where-Object
Command | Where-Object { condition }Group-Object
Command | Group-Object -Property ServiceTypeSort-Object
Command | Sort-Object BasePriorityForEach-Object
Command | ForEach-Object{
statement
}Format output
Command | Format-list -property a,b,c
Command | Format-Table -property a,b,c
Command | Format-Table -property a,b,c -wrap- e.g
Get-Process | Sort-Object cpu -Descending | Select-Object -First 10 | Format-Table processname, id, cpu, ws, pmConvert Objects
ConvertTo-Html
ConvertTo-Csv
ConvertTo-Json
ConvertTo-Xml- e.g.
Get-Service | ConvertTo-Html -Property name, displayname, status
Get-Process | Sort-Object cpu -Descending | Select-Object -First 10 | ConvertTo-Html -Property name, cpu
Get-ChildItem -Recurse | ConvertTo-Html -Property fullname, name | Out-File file_list.htmlClass
# Ex 1
class Student{
[String]$name='NA'
[int32]$age='0'
}
# Ex 2
class MathClass{
[int32]$number1 = 0
[int32]$number2 = 0
#method
[int32] Addtition(){
return $this.number1 + $this.number2;
}
[int32] Subtraction(){
return $this.number2 - $this.number1;
}
}Save remote object for troubleshoot
Get-Process | Select-Object -Last 4 | Export-Clixml 'to_debug.xml'
$save_processes = Import-Clixml .\to_debug.xmlInstall software
Start-Process $installation_file /S -NoNewWindows -Wait -PassThruCreate folder
New-Item $destination_dir -ItemType Directory -ForceDownload file from internet
Invoke-WebRequest -Uri $source -OutFile $destination_file -VerboseLogging
Start-Transcript -Path "path\to\file.log" -NoClobber
# -NoClobber = No over write
Stop-TranscriptGet Execution Policy
Get-ExecutionPolicySend email
mmc.exe
ISE Snippet
Snippet is saved at C:\Users\username\Documents\WindowsPowerShell\Snippets
New-IseSnippet -Tilte Command-Name -Description "do sth" -Text @'
# Write reuseable logic here
'@
Get-IseSnippetWeb Scrapping
$webRequest.ParsedHtml | gm
#Download a file
## method 1
$url = "website"
$web_client = New-Object System.Net.WebClient
$web_client.DownloadFile($url, "Path\to\File.extension")
## method 2
Invoke-WebRequest $url -OutFile "Path\to\File.extension"Execute external scripts
$command = 'Get-Process | Where {$_.cpu -gt 1000}'
Invoke-Expression $command
# from web:
$script = Invoke-Webrequest https://link/to/sript.ps1
Invoke-Expression $($script.Content)Interact with database SQL
$VLC_URL = "https://get.videolan.org/vlc/last/win64/"
$GET_HTML = Invoke-WebRequest $VLC_URL
$FILE = $GET_HTML.Links | Select-Object @{Label='href';Expression={@{$true=$_.href}[$_.href.EndsWith('win64.exe')]}} | Select-Object -ExpandProperty href
$URL = ($VLC_URL+$FILE)
$DOWNLOAD_DIR = "C:\users\qwiklabs\Downloads\"
$OUTPUT_FILE = ($DOWNLOAD_DIR+$FILE)
(new-object System.Net.WebClient).DownloadFile($URL, $OUTPUT_FILE)
cmd.exe /c $OUTPUT_FILE /S
Compress-Archive -Path Earth, Mercury, Venus Planets.zip