PowerShell
Sample
## grep Select-String -Path .\sample.txt -Pattern "foo" ## count (ipconfig | Select-String IPv4 | Measure-Object).Count ## find ls -r | where { $_.Name -match "foo" } ls | where { ($(Get-Date) - $_.LastWriteTime).Days -lt 30 } | where { $_.Length -gt 1KB } ## cat Get-Content .\sample.txt ## sort | uniq Get-Content .\sample.txt | Sort-Object | Get-Unique ## wc -l $(cat .\sample.txt).Length ## diff Compare-Object $(cat .\foo.txt) $(cat .\bar.txt) ## sed Get-Content .\foo.txt | % { $_ -replace "posh", "PowerShell" } ## tail Get-Content .\error.log -Wait Get-Content .\error.log -Wait -Tail 5 ## wget (new-object net.webclient).DownloadFile("URL", "Output_Path") Invoke-WebRequest http://localhost/vim.zip -OutFile .\vim.zip ## curl Invoke-RestMethod -Uri "http://localhost:54321/poshcomplete/" -Method POST -Body "text=test" ## unzip [System.IO.Compression.Zipfile]::ExtractToDirectory("Zip_Path", "Extract_Path") ## xargs ls .\ | % { cat $_.FullName } ## timer $n = 3 timeout $($n * 60); shutdown -h ## calc 1 + 2 + [Math]::Pow(2, 10) ## size gci .\ | where { $_.PSIsContainer } | % { Write-Host -NoNewline ($_.Name + "`t"); (gci $_.Name -Recurse | measure -Property Length -Sum).Sum; } ## history Get-History | % { Out-File -InputObject $_.CommandLine -Encoding default -Append ~/foo.ps1 } ## eject (New-Object -com WMPlayer.OCX.7).cdromCollection.Item(0).Eject()
File Hash
https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-filehash
Get-FileHash * -Algorithm SHA1 Get-FileHash * -Algorithm SHA256 Get-FileHash * -Algorithm SHA384 Get-FileHash * -Algorithm SHA512 Get-FileHash * -Algorithm MD5
List of Packages
https://learn.microsoft.com/powershell/module/packagemanagement/get-package
Get-Package Get-Package | ft -AutoSize
Output Format
https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/format-table
https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/format-list
Get-Package | ft -AutoSize
Rename
dir | rename-item -newname { $_.name -replace '[0-9][0-9]', '' }
Loop
$ns = 1 $ne = 10 for ( $idx = $ns; $idx -le $ne; $idx++ ) { $num = $idx.ToString('000') $target = "sample" + $num + ".dat" }
Grep and Replace
$path = "C:\Users\user\Desktop\project" $regex = 'org' $replace = 'rep' Get-ChildItem -Path $path -Recurse | ? { Select-String -InputObject $_ -Pattern $regex } | % { $file = $_ $content = Get-Content -LiteralPath $file Set-Content $_.FullName ($content -replace $regex, $replace) }
Loop for All Files
$path = "C:\Users\user\Desktop\project" $array = dir $path foreach ( $file in $array ) { echo $file (cat $file.FullName | Select-String "rep").Count } $array = @(100, 120, 300) foreach ( $dat in $array ) { $sum += $dat } Write-Output $sum
Acknowledgments
Daiphys is a professional-service company for research and development of leading-edge technologies in science and engineering.
Get started accelerating your business through our deep expertise in R&D with AI, quantum computing, and space development; please get in touch with Daiphys today!
Daiphys Technologies LLC - https://www.daiphys.com/