Guide
Practical PowerShell for IT and Security
PowerShell is the main lever for administering Windows, Active Directory, and Entra at scale. It is also one of the most-used attacker tools, because it is already installed, trusted, and powerful. This guide covers the patterns I use for real admin work, then the logging and hardening that let you keep the benefit while catching the abuse.
It assumes you know the basics of a cmdlet and a variable. Examples use
corp.local for on-premises and example.co.uk for the cloud
tenant.
Core
The Pipeline and Filtering
Almost everything is a pipeline: get objects, filter them, select the fields you want. The one rule worth internalising is to filter as early as possible.
Get-ADUser -Filter * -Properties LastLogonDate |
Where-Object { $_.Enabled -eq $true } |
Select-Object Name, LastLogonDate |
Sort-Object LastLogonDate
Filter left: use the cmdlet's own -Filter where possible. It runs server-side and returns far less data than pulling everything and filtering with Where-Object.
Select-Object: trims objects to the fields you need, which keeps output readable and exports clean.
Admin
Active Directory
The ActiveDirectory module covers most daily tasks. A few patterns that come up constantly:
# Find locked-out or disabled accounts
Search-ADAccount -LockedOut
Search-ADAccount -AccountDisabled
# Users who haven't logged on in 90 days
Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonDate |
Where-Object { $_.LastLogonDate -lt (Get-Date).AddDays(-90) }
# Add a user to a group
Add-ADGroupMember -Identity "IT-Staff" -Members jane.doe
For bulk changes, pipe a filtered set into the change cmdlet rather than looping by hand,
and test with -WhatIf first so you see what would change before it does.
Admin
Entra ID with Microsoft Graph
The Microsoft Graph PowerShell module is the current way to manage Entra. Connect with only the scopes you need, then query.
Connect-MgGraph -Scopes User.Read.All
Get-MgUser -Filter "userPrincipalName eq 'jane.doe@example.co.uk'" `
-Property DisplayName, UserPrincipalName, OnPremisesSyncEnabled
Least-privilege scopes: request only the permissions the task needs, not the broadest ones. The consent is logged against your account.
Pairs with my Entra Connect sync guide, which uses these cmdlets to check the cloud side of a sync issue.
Scale
Remoting
Run commands across many machines without logging into each. Use it for one-off tasks and health checks.
# Run on several hosts at once
Invoke-Command -ComputerName SRV01, SRV02 -ScriptBlock { Get-Service -Name Spooler }
# Interactive session on one host
Enter-PSSession -ComputerName SRV01
Remoting is powerful, which is exactly why it is also an attacker's lateral-movement path, so the logging and limits below matter as much as the convenience.
Security
Logging and Hardening
PowerShell is a favourite for living-off-the-land attacks. You cannot remove it, so the job is to log what it does and limit what it can do. Set these through Group Policy across the fleet.
Script-block logging: records the actual code run, including obfuscated and in-memory scripts, as Event ID 4104. The single most useful setting.
Module logging and transcription: 4103 for pipeline detail, and a written transcript of each session for review.
Constrained Language Mode: blocks the dangerous features (arbitrary .NET, Add-Type) while leaving normal cmdlets working.
JEA (Just Enough Administration): role-limited endpoints so an operator can run only the specific commands their role needs.
Remove PowerShell v2: it has no script-block logging and is used to downgrade around these controls.
AMSI: the Antimalware Scan Interface lets Defender inspect scripts at runtime, including in memory.
One myth to drop: Execution Policy is not a security control. It stops an accidental double-click, not an attacker, who can bypass it in one flag. The real controls are the logging and language limits above.
Why It Matters
The Takeaway
PowerShell is the clearest example of a tool that is equally an administrator's and an attacker's. The same remoting that patches a fleet moves an intruder across it. Knowing it well means using it to work faster and recognising its abuse in the logs: script-block events feed the detections in my Splunk detection lab and sit in my Windows event ID reference.