Powershell Cheat Sheet

Hello World

Write-host "hello world!"

Files and Folders

These are some example of working with files and folders

Create folder and file

New-Item -Path "C:\Windows\Tasks\Test Folder" -ItemType Directory #Create Folder
New-Item -Path "C:\Windows\Tasks\Test.txt" -ItemType File #Create File
Set-Content "C:\Windows\Tasks\Test.txt" "Hello World" #Write to file
Add-Content "C:\Windows\Tasks\Test.txt" "Hello Redtm" #Append to file
Get-Content "Test.txt" #Read File
Clear-Content "C:\Windows\Tasks\Test.txt" #Erase anything in the Test.txt

Copy/Move/Delete

Copy-Item "Test.txt" "Test2.txt" #Copy File or folder
Remove-Item "Test.txt" #Remove File or folder
Rename-Item "test.txt test2.txt"

Other CMDLET

Get-History #Display Command History
Get-History -count 10 #Display first 10th history
Invoke-Item "test.txt" #Default behavior. So it opens test.txt in notepad
Get-Process | Select-Object -Property ProcessName, Id #Select specific propertise
Get-Service | Where-Object "ServiceName" #Get specific object/service
"a","a","c"|Get-Unique #Only Display uniq content
Import-Module PowerView.ps1 #Adds modules to the current session.

Variables

Variables used to store data to use in future by referencing to the variable name! There are 4 type of variable we can use in bash. We can’t use Reserved word for variable names. Reserved words are:

  1. Integer: These are numeric variables
  2. String: “Text based variable”
  3. Array: “Index of variable”

Integer Variable

$var1=1
write-host $var1

String Variable

String variable is double quoted

$var1="Hello redtm"
write-host $var1

Command Substitutions

Two we can substitute command

  1. Using double backqoute “`Command_Here`”
  2. Using $(Command_here)
##Using backquote
passwd=`cat /etc/passwd`

##Using $ sign with first bracket
shadow="$(cat /etc/shadow)" #Without root, permission denied
issue="$(cat /etc/issue)"

#display contents
echo $passwd 
echo $shadow
echo $issue

Operators

Here is the some list of commonly used operators

Arithmetic

OperatorExampleDescription
+r+rrAddition: 10+10=20
r-rrSub: 10-10=0
*a*aMultiplication: 2*2=4
/a/abDivision: 4/2=2

Assignment

OperatorExampleDescription
=$var1=1337Simple Assignment
+=var1+=var2Add and Assignment
-=var1-=var2Subtract and Assignment

Comparision

OperatorExampleDescription
eq2 – eq 2If equal
ne2 -eq 3Not Equal
gt2 -gt 1Greater than
ge2 -ge 2Greater than, or equal
le2 -le 4Less than or equal
lt2 -lt 4Less than

Reference

Arrays

Store collection of data.

$arry = 1,2,3,4 #Declare array
$arry.Count #How many?
$arry.Length #How many?
$arry[0] #access array which 1

To reference the array we need to use $array_name[index]

Conditional

Conditional is a test. This mean, parametrically test for Fail and Success Status and divert the execution somewhere! Bash also has primary expression for conditional statements!

if

if ($a -eq "redtm") {
    Write-Host "Welcome to redtm!"
}

if/else

if ($a -eq "redtm") {
    Write-Host "Welcome to redtm!"
}
else {
    Write-Host "You were looking something else!"
}

Loop

For Loop

$service = @("pentesting","red teaming","script kiddie")
for($s=0;$s -lt service.count;$s++){
    Write-Host $service[$s]
}

ForEach Loop

$service = @("pentesting","red teaming","script kiddie")
foreach ($s in $service){
    $service
}

While Loop

$service = @("pentesting","red teaming","script kiddie");
$c=0;
while($c -lt $service.length){
    if($service[$c]="script kiddie"){

        Write-Host "You are script kiddie"
        break;
    }
    $c+=1

}

Functions

Function declared using function keyword.

function Get-Redtm {
    Write-Host "This is redm function"
}

Get-Redtm #Call the function

Learn More

Microsoft Documentation!