The Windows Console: CMD and PowerShell
As soon as you get a shell on a Windows box, everything you do goes through one of its two consoles: CMD or PowerShell. Being comfortable in them is the difference between enumerating a machine in two minutes and fighting for half an hour with Linux commands that don’t exist. And PowerShell isn’t “another Bash”: it works with objects, not text, and that changes how you filter and search.
Concepts
Section titled “Concepts”CMD and PowerShell
Section titled “CMD and PowerShell”| CMD | PowerShell | |
|---|---|---|
| Origin | MS-DOS | .NET (2006) |
| What goes through the pipe | Text | Objects |
| Prompt | C:\> |
PS C:\> |
| Reach | Basic commands and .bat |
The whole system: registry, services, WMI/CIM, network, AD |
| Version | Just one | 5.1 (ships with Windows) and 7 (cross-platform, installed separately, binary pwsh) |
The .exe programs you use in CMD (ipconfig, whoami, net user…) work the same inside PowerShell. The built-in commands of cmd.exe don’t: dir /s, set, where or mklink are either aliases for cmdlets with a different syntax or don’t exist at all (for those, use cmd /c …). And cmdlets only exist in PowerShell.
Cmdlets: Verb-Noun
Section titled “Cmdlets: Verb-Noun”PowerShell commands are called cmdlets and always take the form Verb-Noun. The verb comes from a closed list (Get reads, Set changes, New creates, Remove deletes, Start/Stop…), so you can guess names: if Get-Service lists services, Stop-Service stops them.
| To | Cmdlet |
|---|---|
| Find a command by keyword | Get-Command *firewall* |
| Every verb for a noun | Get-Command -Noun Service |
| Help with examples | Get-Help Get-Process -Examples |
| What an alias points to | Get-Alias ls |
Equivalents
Section titled “Equivalents”| Linux | CMD | PowerShell |
|---|---|---|
ls |
dir |
Get-ChildItem |
cd · pwd |
cd · cd |
Set-Location · Get-Location |
cat |
type |
Get-Content |
cp · mv · rm |
copy · move · del |
Copy-Item · Move-Item · Remove-Item |
grep |
findstr |
Select-String |
which |
where |
Get-Command |
ps · kill |
tasklist · taskkill |
Get-Process · Stop-Process |
man |
command /? |
Get-Help |
tail -f |
— | Get-Content -Tail 10 -Wait |
env |
set |
Get-ChildItem env: |
Shared by all of them: whoami, hostname, ping, nslookup, arp -a, netstat and, since Windows 10 (version 1803), ssh, scp, curl.exe and tar.
The object pipeline
Section titled “The object pipeline”In Bash, ps aux | grep root filters lines of text, and if the output format changes, the filter breaks. In PowerShell what travels through | are objects with properties, and you filter by property name.
Get-Member tells you which properties an object has:
PS C:\> Get-ChildItem C:\Windows\notepad.exe | Get-Member -MemberType Property
TypeName: System.IO.FileInfoName MemberType Definition---- ---------- ----------FullName Property string FullName {get;}LastWriteTime Property datetime LastWriteTime {get;set;}Length Property long Length {get;}...Now you know what you can ask for. The three pieces you’ll use the most:
| Cmdlet | Does | Example |
|---|---|---|
Where-Object |
Filters | Where-Object Length -gt 1MB |
Select-Object |
Picks properties or the first N | Select-Object Name, Length -First 5 |
Sort-Object |
Sorts | Sort-Object LastWriteTime -Descending |
Comparison operators are words: -eq, -ne, -gt, -lt, -like (wildcards) and -match (regular expression). You don’t use > because > redirects to a file, just like in Bash.
Scripts and variables
Section titled “Scripts and variables”- Variables start with
$and hold objects, not text:$procs = Get-Process. - Inside
Where-ObjectandForEach-Object,$_is the object currently going through the pipeline. - A script is a
.ps1file and you run it by its path:.\script.ps1(the.\is mandatory).
foreach ($svc in Get-Service) { if ($svc.Status -eq 'Running') { $svc.Name }}# the same in one lineGet-Service | Where-Object { $_.Status -eq 'Running' } | ForEach-Object { $_.Name }Execution Policy
Section titled “Execution Policy”It decides whether PowerShell runs .ps1 scripts. The default is Restricted (none) on Windows 10 and 11 and RemoteSigned on Windows Server (scripts downloaded from the internet must be signed).
Get-ExecutionPolicy -Listshows it per scope.powershell -ExecutionPolicy Bypass -File .\script.ps1skips it for that run, no admin rights needed, unless the policy is enforced by GPO (theMachinePolicyandUserPolicyscopes take precedence over the parameter).
Microsoft says it plainly: it is not a security boundary, just a safeguard against running something by accident. Even with a GPO in place, a script’s contents can still be read and run other ways (for example, by piping them to Invoke-Expression).
The PSReadLine history
Section titled “The PSReadLine history”Get-History only shows the current session. But the PSReadLine module saves to disk almost everything that gets typed, across all sessions (up to 4096 lines by default). Recent PSReadLine versions, such as the one in PowerShell 7, skip lines containing password, token, secret, apikey or asplaintext; the one bundled with Windows PowerShell 5.1 is older:
PS C:\> (Get-PSReadLineOption).HistorySavePathC:\Users\ana\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txtLine endings: CRLF
Section titled “Line endings: CRLF”Windows ends lines with CRLF (\r\n); Linux uses only LF. A Bash script saved on Windows fails on Kali with nonsense errors such as $'\r': command not found. Fix it with dos2unix script.sh (or sed -i 's/\r$//' script.sh).
In practice
Section titled “In practice”# Discover without leaving the consolePS C:\> Get-Command *process* # which cmdlets mention "process"?PS C:\> Get-Help Stop-Process -Examples # usage examplesPS C:\> Get-Alias curl # in 5.1: curl -> Invoke-WebRequest
# Top 5 processes by memoryPS C:\> Get-Process | Sort-Object WS -Descending | Select-Object -First 5 Name, Id, WS# WS = Working Set, memory in bytes
# Files modified in the last week under the profilePS C:\> Get-ChildItem $env:USERPROFILE -Recurse -File -ErrorAction SilentlyContinue |>> Where-Object LastWriteTime -gt (Get-Date).AddDays(-7) |>> Select-Object FullName, LastWriteTime# -ErrorAction SilentlyContinue hides the "access denied" noise# Search text in files (PowerShell's grep -ri)PS C:\> Get-ChildItem C:\inetpub -Recurse -Include *.config, *.xml -ErrorAction SilentlyContinue |>> Select-String -Pattern 'password' |>> Select-Object Path, LineNumber, Line# Returns file, line number and the matching line:: The same in CMDC:\> dir /s /b C:\inetpub\*.configC:\> findstr /s /i /n "password" C:\inetpub\*.config:: /s subfolders · /i case-insensitive · /n line number# Current user's PowerShell historyPS C:\> Get-Content (Get-PSReadLineOption).HistorySavePath -Tail 20
# Running a script while the policy is RestrictedPS C:\> .\enum.ps1# .\enum.ps1 cannot be loaded because running scripts is disabled on this system.PS C:\> powershell -ExecutionPolicy Bypass -File .\enum.ps1Next offensive step
Section titled “Next offensive step”- Enumerate a compromised Windows host with these tools: Windows enumeration.
- Why PowerShell is the most watched thing on the system (AMSI, Script Block Logging, Constrained Language Mode) and how to work with that: Windows evasion.
- Escalate privileges from what you find: Windows privilege escalation.
Cheat sheet
Section titled “Cheat sheet”| Concept | What to remember |
|---|---|
| CMD · PowerShell | Text · objects. .exe programs work in both; CMD built-ins don’t. |
| Verb-Noun | Get reads, Set changes, New creates, Remove deletes. |
Get-Command *word* |
Find the cmdlet you need. |
Get-Help -Examples |
man with examples. |
| Get-Member |
Which properties the pipeline object has. |
Where · Select · Sort |
Filter · pick · sort. |
-gt, not > |
> is redirection. |
$_ |
The current object inside Where-Object and ForEach-Object. |
.\script.ps1 |
The .\ is mandatory. |
| Execution Policy | Not security. -ExecutionPolicy Bypass. |
HistorySavePath |
History of every session: look for passwords. |
sc · curl |
Trap aliases in PowerShell 5.1: use sc.exe and curl.exe. |
| CRLF | dos2unix before running it on Linux. |
Review questions
Section titled “Review questions”-
What is the fundamental difference between the Bash pipeline and the PowerShell pipeline?
Answer
In Bash, lines of text flow through it and you filter by cutting text (
grep,cut,awk), which breaks if the format changes. In PowerShell, objects with properties flow through it and you filter by property name (Where-Object Length -gt 1MB), regardless of how they are displayed. -
You type
sc queryin PowerShell and it doesn’t list any services. Why, and how do you fix it?Answer
Because in Windows PowerShell 5.1
scis the alias forSet-Content(PowerShell 7 removed it). Typesc.exe queryinstead (or useGet-Service), which works in both. -
You want the processes using more than 100 MB of memory. How do you write it, and why not
> 100MB?Answer
Get-Process | Where-Object WS -gt 100MB. You don’t use>because in PowerShell, as in Bash,>redirects output to a file: you would create a file named100MB. -
You don’t know which properties
Get-Servicereturns. How do you find out?Answer
Get-Service | Get-Member(orGet-Service | Select-Object -First 1 | Format-List *to also see the values). -
A
.ps1script won’t run because “running scripts is disabled”. Does that stop you during a pentest?Answer
No. Execution Policy isn’t a security boundary:
powershell -ExecutionPolicy Bypass -File .\script.ps1skips it for that run without admin rights. If the policy is enforced by GPO the parameter isn’t enough, but the script’s contents can still be run other ways. -
Why isn’t
Get-Historyenough to review what a user has typed?Answer
Because it only shows the current session. The history of every session is saved by PSReadLine in the file given by
(Get-PSReadLineOption).HistorySavePath, usuallyConsoleHost_history.txtunderAppData\Roaming. -
You copy a Bash script from Windows to your Kali and it fails with
$'\r': command not found. What happened?Answer
The file has Windows line endings (CRLF) and Bash reads the
\ras part of the commands. Fix it withdos2unix script.sh.