Lokasi

https://github.com/PowerShell/PowerShell https://ss64.com/ps/

Telemetry

$env:POWERSHELL_TELEMETRY_OPTOUT=1

Eksekusi

Dari cmd.exe kita panggil

PowerShell.exe -Command \'perintahnya\'

Profile

Lokasi profile seperti .bashrc

$profile

Biasanya ke %USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Execution Policy

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Set-ExecutionPolicy Restricted

Perintah Umum

Script

Jalankan perintah dalam quotes

iex "C:\Something\com.exe arguments"

&"C:\Program Files\Adobe\Office\Word.exe" arguments

Prompt

Ubah prompt dengan ubah fungsi Prompt()

function prompt {$ESC = [char]27; $LOC = (Get-Location).Path.Split('\');"PS aldo@" + $env:COMPUTERNAME + " "+ "$ESC[32m..\" + $LOC[-2] + "\" + $LOC[-1] + "$ESC[0m" +"➤ "}

function prompt {
    $ESC = [char]27;
    $LOC = (Get-Location).Drive.CurrentLocation.Split("\");
    $SB = New-Object -TypeName System.Text.StringBuilder;
    $_ = ($LOC | Where-Object {$_ –ne (Split-Path -Leaf (Get-Location))} | ForEach {$SB.Append($_[0]+"\")})
    if ($LOC.length -gt 2) {
        $LOCS = (Get-Location).Drive.Root + $SB.ToString() + $LOC[-1];
    } else {
        $LOCS = (Get-Location).Path
    }
    "PS7 aldo@" + $env:COMPUTERNAME + " "+ "$ESC[32m" + $LOCS + "$ESC[0m" +"➤ "
}

Get-Content Function:\prompt

Tulis teks berwarna

Write-Host -NoNewline -ForegroundColor green text

Grep

get-content file.txt | where { $_ -match "expr"}

select-string file.txt -pattern "expr" -casesensitive

Sed

get-content file.txt | %{$_ -replace "expr", "replace"}

(get-content file.txt) -replace 'expr','replace' | out-file -encoding ASCII file.txt

Tail

get-content file.txt -wait -tail 20

Transcript

Start-Transcript
'...'
Stop-Transcript

Output

Jangan pakai >, nanti jadi UTF16LE.

command | Out-File -Encoding utf8 .\output.txt

## atau set variabel
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'

Pipestream

Mount-DiskImage -ImagePath 'D:\linux.iso' | fc
Mount-DiskImage -ImagePath 'D:\linux.iso' | Select-Object -Property DevivePath
Get-DiskImage -DevicePath \\.\CDROM0 | fc
Get-Volume |%{$d=$_.DriveLetter;$_ |Get-DiskImage |select @{Name='DriveLetter';Expression={$d}},ImagePath}