PowerShell guardrails for admin workstations
Nathan Chadwick · · 7 min read · Updated
Listen
TLDRRead the short version
- Turn on script block logging for Windows PowerShell 5.1 and for PowerShell 7, and send both logs off the box. Protected Event Logging exists because those logs contain secrets.
- Execution policy is not a security boundary. Constrained language comes from App Control, not from an environment variable.
- Block remote PowerShell from machines that are not an admin workstation. A daily laptop with mail and a browser is the wrong place to hold a privileged session.
- Just Enough Administration hands someone one task. A wildcard in the role capability is a remote shell with extra steps.
- Time-bound elevation beats standing local admin. Pair it with Privileged Identity Management where the licence includes it.

The shell on the same laptop as the inbox#
Somewhere in the estate, a Global Administrator opens PowerShell on the laptop they also use for mail and a browser. The script works. Six weeks later something has happened, and the question is what that shell ran. The laptop has no script block log, PowerShell 7 was never included in the policy that covered 5.1, and the transcript, if it exists, is a file the user can delete.
PowerShell is a normal admin tool and a normal attacker tool. The guardrails are not "ban the shell". They are: record it, constrain the sessions that do not need all of it, and stop privileged sessions living on the machine that gets phished. The short version of this post said logging, a hardened admin tier, and time-bound elevation. This is those three, in enough detail to implement.
Logging that exists before you need it#
Turn on script block logging. Event 4104 is the one you will search. It has to exist in two places, because they are two engines:
- Windows PowerShell 5.1 writes to Microsoft-Windows-PowerShell/Operational.
- PowerShell 7 writes to PowerShellCore/Operational.
A policy that only covers Windows PowerShell 5.1 leaves pwsh.exe quiet. Attackers are fine with that. Enable script block logging for both, and enable module logging for the modules your admins actually use, so the pipeline is there as well as the script text.
Microsoft recommends Protected Event Logging when script block logging is used for anything other than diagnostics. The log records the script block. Scripts contain passwords, tokens, and connection strings. Protected Event Logging encrypts those events to a certificate you hold, so the SIEM copy is not a second password spreadsheet.
Transcription is the other copy: commands and output, in a file. Put it on a path the operator cannot quietly clear, or ship it as it is written. A transcript in the user's documents folder is a diary they can edit.
Then get the events off the machine. Windows Event Forwarding, the agent you already run, a connector into the SIEM. A log that only exists on the laptop is gone when the laptop is the thing you are investigating. Someone who is already an administrator can tamper with local logging. That is not a reason to skip it. It is a reason the copy leaves the box quickly, and a reason the person was not an administrator on that box in the first place.
Disable Windows PowerShell 2.0 where it is still an optional feature. Version 2 does not produce this logging. Leaving it installed is a downgrade.
Execution policy is not the control#
The execution policy is not a security boundary. Microsoft says so in the same page as the feature. A user who cannot run a script can paste it. powershell.exe -ExecutionPolicy Bypass is not clever. Set a sensible policy so people do not run random files by double-clicking. Do not put "we set RemoteSigned" on a risk slide.
Constrained language mode is the real reduction in what an untrusted script can do: fewer .NET and COM tricks, which is how a lot of commodity tooling loads. It is applied when PowerShell detects an application control policy, AppLocker or App Control for Business, and the script is not one of the trusted ones. The language mode documentation is blunt that you want this under application control so it cannot simply be switched off.
Two limits, so this does not get oversold:
- An environment variable is not the control.
__PSLockdownPolicyis a debugging switch. A person who can set it can unset it. - Constrained language does not make
powershell.exeharmless. A trusted engine in a trusted path is still a living-off-the-land binary. I wrote about that grey area in the post on attackers using your own tools. Application control plus logging is the pair. Either one on its own is a story you tell yourself.
A session that is not the whole shell#
Just Enough Administration is how you hand a person one job. A JEA endpoint has a role capability (the cmdlets, parameters, and modules they may use) and a session configuration (who may connect, and which account the commands run as).
The shape of a role capability is a whitelist. Restart the print spooler means Restart-Service with Name limited to Spooler. It does not mean VisibleCmdlets = '*'. A wildcard is a remote shell with extra steps. Review the file the way you would review a firewall rule.
The account that runs the command matters as much as the menu. A virtual account that is a local administrator can still do anything the menu allows, and a loose menu allows a lot. Prefer a role that can do the delegated task and nothing adjacent. Turn transcription on for that endpoint. Test it with the account that will actually connect, not with your own, which is probably outside the group.
JEA is for delegated work: the service desk, the app owner, the person who needs one command on a schedule. It is the wrong tool for the engineer redesigning the platform. That engineer gets a full shell, on a machine built for it, for a limited time.
The machine the session runs on#
Block remote PowerShell from computers that are not an admin workstation.
WinRM listens on 5985 and 5986. Ordinary endpoints do not need to offer it to other ordinary endpoints, and servers do not need to offer it to the general workstation VLAN. Allow it from the admin tier. This is the same lateral-movement cut as denying network logon for local accounts, which I went through with Windows LAPS. A stolen everyday laptop should not be a remote shell onto the next laptop.
The admin workstation itself should be dull. A privileged access device does not also browse and read mail on the identity that can change Conditional Access. Cyber Essentials asks for the same separation: admin accounts are not the daily accounts. If a full privileged shell has to happen, it happens there, or through a jump host that records the session, with the daily laptop used to reach that host and not to hold the credential.
Standing local admin on the daily laptop fails this test immediately. The attacker who phishes that user is already an administrator, and your language mode is a speed bump they may not even need.
Time-bound elevation, and scripts that ship#
Time-bound beats standing. For Entra roles and Azure roles, Privileged Identity Management is the product, where you have Entra ID P2. Eligible, not permanently active. A short window. Approval and a ticket number on the roles that can change identity or the vault. Business Premium and E3 do not include PIM. Do not write a standard you cannot license. Where you cannot, a written break-glass and a small number of named admins still beats a role assigned to a team group of forty.
Pair that with JEA for the tasks that should never have been a role activation in the first place. Restarting a service is not Global Administrator.
Scripts you deploy are a different approval path, and they are easy to forget because they are "just a script":
- An Intune platform script or remediation that runs as SYSTEM is production. Review it. Store it somewhere the people who can run it cannot quietly edit it.
- Ship it with
SupportsShouldProcessand run the report-only pass first. The profile cleanup script is the pattern: a week of WhatIf, then delete. - Do not put a password in the script body. That body is now in the 4104 log, on every device, and in any Intune admin's browser. If a task needs a secret, the secret lives in a vault and the script fetches it for the duration of the run.
A short checklist#
- Script block logging and module logging enabled for Windows PowerShell 5.1 and PowerShell 7.
- Protected Event Logging on, so secrets in those events are encrypted.
- Events forwarded off the box. Transcription kept where the operator cannot edit it.
- PowerShell 2.0 removed.
- Execution policy set to something sensible, and not claimed as a control.
- Constrained language coming from App Control or AppLocker, tested with an untrusted script in a user-writable path.
- JEA for delegated tasks, with an explicit cmdlet list and a test as the delegated user.
- WinRM unreachable from ordinary endpoints. Privileged shells on an admin workstation.
- PIM for privileged roles where the licence exists. No standing local admin on daily machines.
- Production scripts reviewed, WhatIf first, no embedded passwords.
Closing#
A privileged PowerShell session is a capability, and capabilities get stolen with the laptop they were left on. Record both engines. Constrain the sessions that only needed one cmdlet. Keep the rest on a machine that does not also open the mail.
The log will not save you if nobody is an administrator of the thing that got phished. The log is how you prove what happened after the elevation was supposed to be the rare case.
References#
Sources and further reading for the claims above.
- Microsoft Learn, PowerShell logging on Windows
- Microsoft Learn, about_Execution_Policies
- Microsoft Learn, about_Language_Modes
- Microsoft Learn, App Control rule types
- Microsoft Learn, Just Enough Administration (JEA)
- Microsoft Learn, Privileged access devices
- Microsoft Learn, Privileged Identity Management
- Microsoft Learn, Everything about ShouldProcess
- MITRE ATT&CK T1059.001, PowerShell