PowerShell for Security
Use PowerShell to query Windows systems, event logs and Active Directory, and understand why attackers love it too.
Beginner · about 12 hours · difficulty 2/5
Learn first: Windows Internals for Defenders
PowerShell is the admin language of Windows. Defenders use it to hunt across endpoints and pull event logs; attackers use it because it is already installed and powerful. Knowing it well helps on both sides.
Core concepts
- Commands are cmdlets named Verb-Noun:
Get-Process,Get-Service,Get-ChildItem. - Pipelines pass objects, not text:
Get-Process | Where-Object CPU -gt 100 | Select-Object Name, Id. Get-HelpandGet-Membershow what a command does and what properties an object has.- Scripts are
.ps1files; execution policy controls whether they run (it is a safety net, not a security boundary).
Security uses
Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4625}to pull failed logons.Get-LocalUser,Get-ScheduledTask, and registry queries for persistence hunting.- The ActiveDirectory module (
Get-ADUser,Get-ADGroupMember) for auditing privileged groups.
Defending against malicious PowerShell
Attackers use encoded commands (-EncodedCommand), download cradles and in-memory execution. Defenders enable Script Block Logging (event 4104), Module Logging, Constrained Language Mode and AMSI, then alert on suspicious patterns.
Pitfalls
- Treating output as text and parsing it with regex when you could just select object properties.
Good enough to move on: you can write a script that exports the last 24 hours of failed logons to CSV grouped by account and source.
Key concepts
- Cmdlets and Verb-Noun naming
- Object pipelines
- Get-WinEvent for log queries
- ActiveDirectory module
- Script Block Logging and AMSI
- Execution policy
Other options for this step
- Python for Security: Learn enough Python to parse logs, call APIs, work with files and sockets, and write your own small security tools.
- Bash Scripting for Security: Automate Linux tasks with Bash scripts: loops, conditionals, pipelines and quick one-off tools for log triage and recon in labs.
Mini project
Write a PowerShell script that lists autostart entries (Run keys, services, scheduled tasks) on a machine and exports them to CSV for baseline comparison.
Check yourself
Why is PowerShell execution policy not considered a security control?
Free resources
- PowerShell documentation (Microsoft)
- Get-WinEvent reference (Microsoft)