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:
- Integer: These are numeric variables
- String: “Text based variable”
- 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
- Using double backqoute “`Command_Here`”
- 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
Operator | Example | Description |
---|---|---|
+ | r+rr | Addition: 10+10=20 |
– | r-rr | Sub: 10-10=0 |
* | a*a | Multiplication: 2*2=4 |
/ | a/ab | Division: 4/2=2 |
Assignment
Operator | Example | Description |
---|---|---|
= | $var1=1337 | Simple Assignment |
+= | var1+=var2 | Add and Assignment |
-= | var1-=var2 | Subtract and Assignment |
Comparision
Operator | Example | Description |
---|---|---|
eq | 2 – eq 2 | If equal |
ne | 2 -eq 3 | Not Equal |
gt | 2 -gt 1 | Greater than |
ge | 2 -ge 2 | Greater than, or equal |
le | 2 -le 4 | Less than or equal |
lt | 2 -lt 4 | Less than |
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