Security Power Level: Advanced Chapter 17: PowerShell Security and Hardening
Safe scripting and auditing changes
Safe scripting means previewing a change, using the smallest permission that works, and reading a script before you run it. Auditing means checking afterwards that the change is what you intended.
Lesson 4 of 4 in Chapter 17: PowerShell Security and Hardening. Previous step in this path: Logging and transcription. 3 learning objectives, 5 examples, and a knowledge check.
Start: what you will learn
Safe scripting means previewing a change, using the smallest permission that works, and reading a script before you run it. Auditing means checking afterwards that the change is what you intended.
Learning objectives
- Use WhatIf and Confirm to preview a change
- Review a script before running it
- Audit a change with Get-WinEvent and by re-reading the setting
Concept in simple words
What it is: Safe scripting is a set of habits that keep a change small, visible, and reversible where possible. Least privilege means using the smallest permission that still lets you do the job, no more. Auditing a change means checking afterwards that the setting really changed, and that nothing else changed with it. Preview, permission, reading, and auditing are four separate habits, and together they form one safe routine.
Why it works
Why it works this way: A script can change many items in seconds, so a small mistake can spread fast. A wrong service name, a wrong file path, or a wrong filter can touch far more than you intended, before you notice the error. Previewing a change slows you down just enough to catch that mistake before it happens. Checking afterwards closes the loop, because a command that reports success is not always proof that the right thing changed. The habit is slow on purpose, and that slowness is what keeps a fast tool safe.
Think of it like this
Think of it like this: Previewing is like reading a letter before you post it. Least privilege is like carrying only the keys you need for one room. Auditing is like checking the room after you leave it, to confirm it looks the way you intended. Reading the script first is like reading the letter twice, once for the words, and once for the address on the envelope, because both must be right before you send it.
What WhatIf does behind the scenes: -WhatIf is a common parameter supported by many cmdlets that change state. When you add -WhatIf, PowerShell shows what would happen, without doing it. The output often starts with the words What if, which is your signal that this run is a preview, not a real change. A preview shows the intended target and action, so you can check the target name before the real run. A preview does not guarantee that the real run will succeed, because some problems only appear when the real change is attempted, so treat the preview as a target check, not as a success promise.
What Confirm does behind the scenes: -Confirm is also a common parameter for cmdlets that change state. When you add -Confirm, PowerShell asks for a yes or no answer before it acts, and it waits for your answer. That pause is the whole point. It gives you one more moment to read the target name and the action, and to answer no if either one looks wrong. Answering yes is still your decision, so Confirm only helps if you actually read the prompt instead of answering yes out of habit.
What Get-WinEvent does behind the scenes: Get-WinEvent reads events from Windows event logs, including classic logs such as the System and Application logs. An event is a system record with a time, an event id, and a message. By default, Get-WinEvent returns events newest first, so the most recent event comes first in the result. The -Oldest switch reverses that order, so the oldest event comes first. For auditing, newest first is usually what you want, because you are looking for the event that matches the change you just made.
How it works, step by step
How it works, step by step, in order: First, read the whole script before you run it, and note every command that changes, deletes, or stops something. Second, add -WhatIf to a changing command, which shows what would happen without doing it. Third, use -Confirm where a command should ask before it acts, so you get a pause at the risky step. Fourth, run the real command only after the preview target looks right. Fifth, re-read the affected setting with a read only command, such as Get-Service for a service. Sixth, check recent events with Get-WinEvent. Seventh, compare what you intended, what the setting now shows, and what the event says, and note any mismatch before you move on.
Worked example 1, previewing a service stop: In Stop-Service -Name Spooler -WhatIf, Stop-Service is the command that would stop a service, -Name Spooler names the service, and -WhatIf turns the run into a preview. Look for output that starts with What if, and that names Spooler as the target. The service keeps running during a preview, which is exactly what you want at this stage. If you change Spooler to a different service name, the preview target changes too, so read that name with care. If you remove -WhatIf, the same command becomes a real stop, which is why the presence or absence of that one switch matters so much.
Worked example 2, asking before a service stop: In Stop-Service -Name Spooler -Confirm, Stop-Service would stop a service, -Name Spooler names the service, and -Confirm asks for a yes or no answer before acting. Look for a confirmation prompt that names the action and the target. Answering no means nothing changes, which is the safe answer when the target looks wrong. Answering yes means the stop goes ahead, so read the prompt fully before you answer. If you change -Confirm to -WhatIf, you get a preview with no prompt and no change, which answers a different question from a real run with a pause.
Worked example 3, re-reading the service setting: In Get-Service -Name Spooler, Get-Service reads service information, and -Name Spooler names the service to read. Look for the Status value, such as Running or Stopped, because that value is the fact you are auditing. This step matters because command output alone can be misleading, while the current setting is the truth you need. If you change Spooler to another service name, you audit a different service, so the result tells you nothing about the service you intended to change. If the Status does not match your plan, stop and investigate before you run anything else.
Worked example 4, checking recent system events: In Get-WinEvent -LogName System -MaxEvents 5, Get-WinEvent reads events from a Windows event log, -LogName System names the System log, and -MaxEvents 5 keeps only the five most recent events, so the result stays short and readable. Look for TimeCreated, Id, and Message columns, and look for an event whose time and message match the change you made. If you change System to Application, you read a different log, and the service event you want may not appear. If you change 5 to 1, you see only the single most recent event, which is useful for a quick check but easy to misread if that event is unrelated.
Worked example 5, narrowing the event check to one event: In Get-WinEvent -LogName System -MaxEvents 1 | Select-Object -Property TimeCreated, Id, Message, the first part reads only the single most recent System event, the pipe sends that event onward, and Select-Object -Property TimeCreated, Id, Message keeps only those three properties. Look for one row with a time, an id, and a message, and ask whether that row matches the change you just made. If the row is about a different service or a different time, it is not evidence for your change, even though it is the most recent event. If you change 1 back to 5, you get a short list to compare, which often helps you spot the matching event more reliably.
What changes if you change one part: Change the service name and you preview, confirm, or audit a different target, so the whole routine answers the wrong question. Remove -WhatIf and a preview becomes a real change. Remove -Confirm and a paused run becomes an unpaused run. Change -MaxEvents from 5 to 50 and the result gets longer and harder to read, which makes it easier to miss the event you need. Change -LogName from System to another log and you may conclude that no event exists, when the event is simply in a different log. Small changes in these commands change the meaning, not only the length, of the result.
Edge cases that catch beginners: Not every cmdlet supports -WhatIf and -Confirm. Read only cmdlets do not need them, because they change nothing, and adding a preview switch to a cmdlet that does not support it will give you an error instead of a preview. A WhatIf preview can look perfect and the real run can still fail, for example if permission or state changes between the preview and the real run, so preview again if time has passed or conditions changed. A confirmation prompt can become a habit, where you answer yes without reading, and at that point Confirm stops helping you. Slow down at the prompt on purpose, every time, even for a routine change.
Reading a script before you run it, in practice: Read the script from top to bottom, and mark every line that changes state. Changing verbs to watch for include Stop, Remove, Set, New, and Clear, because those verbs often mean something will be stopped, deleted, set, created, or cleared. Note every file path and every service name, and ask whether each one matches your plan. If a line uses a variable for a target, trace that variable back to where it was set, because the target is the value, not the variable name. If any line is unclear, look it up or ask before you run the script, because an unclear line is a risk, not a detail to skip.
When to use it
When to use it, and when not to: Use WhatIf and Confirm for any change you have not tested, and use least privilege for every routine task, even when the task feels small. Do not use a preview as a substitute for reading the script, because a preview shows the target of one command, not the full plan of a whole script. Do not audit only by looking at the command output, because output can say success while the wrong target changed. Re-read the setting itself, because that is the fact that matters, and check the event log as a second source, because two agreeing sources are stronger than one.
An audit checklist you can reuse: First, write down what you intended to change, in one plain sentence. Second, write down what the setting shows now, from a read only command. Third, write down what the most recent matching event shows, including its time, id, and message. Fourth, compare those three notes. If all three agree, your audit is strong. If the setting and the event disagree, or if either one disagrees with your plan, stop and investigate before you claim success. Keep those notes with the transcript, so a reviewer can follow the same three steps later.
How to spot a mistake in your own thinking: If you trust a script because it came from a source you trust, pause, because trust in a source does not remove the need to read the script. If you skip WhatIf on a first run, pause, because a first run is exactly when a preview helps most. If you audit only the command output, pause, because output is a report, not the setting itself. If you answer a Confirm prompt without reading it, pause, because the prompt is the last chance to catch a wrong target before the change happens. Each pause is cheap, and each missed pause can be costly.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Get-WinEvent -LogName System -MaxEvents 5Example 1
Stop-Service would stop a service. The -Name parameter names the service, and -WhatIf shows what would happen without stopping it, so you can check the target first. Look for output that starts with What if, and check that Spooler is the target.
Stop-Service -Name Spooler -WhatIfIllustrative output: What if: Performing the operation Stop-Service on target Spooler.
Example 2
Stop-Service would stop a service, -Name Spooler names the service, and -Confirm asks for a yes or no answer before acting. Read the prompt fully, answer no when the target looks wrong, and answer yes only when the action and target both match your plan.
Stop-Service -Name Spooler -ConfirmIllustrative output: Confirm: Are you sure you want to perform this action on Spooler?
Example 3
Get-Service reads service information, and -Name Spooler names the service to read. Look for the Status value, such as Running or Stopped, because that current setting is the fact you audit after a change.
Get-Service -Name SpoolerIllustrative output: Status Name DisplayName ------ ---- ----------- Running Spooler Print Spooler
Example 4
Get-WinEvent reads events from a Windows event log. The -LogName parameter names the log, System, and -MaxEvents 5 keeps only the five most recent events, so the result stays short and readable. Look for a time, id, and message that match your change.
Get-WinEvent -LogName System -MaxEvents 5Illustrative output: TimeCreated Id Message ----------- -- ------- 3 November 2026 10:00 7036 The Spooler service entered the running state.
Example 5
The first part reads only the single most recent System event, the pipe sends that event onward, and Select-Object keeps only TimeCreated, Id, and Message. Check whether that one row matches your change, and widen the count when the single row is unrelated.
Get-WinEvent -LogName System -MaxEvents 1 | Select-Object -Property TimeCreated, Id, MessageIllustrative output: TimeCreated Id Message ----------- -- ------- 3 November 2026 10:00 7036 The Spooler service entered the running state.
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Running a changing script without reading it, because it came from a source you trust.
- Skipping WhatIf on a first run, and learning about a wrong target only after the change happened.
- Auditing only the command output, instead of re-reading the setting that was supposed to change.
- Treating a WhatIf preview as proof that the real run will succeed, when a preview only checks the intended target and action.
- Answering a Confirm prompt with yes out of habit, without reading the action and target in the prompt.
- Adding WhatIf or Confirm to a read only cmdlet and expecting a preview, when those switches only apply to cmdlets that change state.
- Auditing from only one source, instead of comparing the intended change, the re-read setting, and the matching event together.
Knowledge check
Knowledge check
What does -WhatIf do?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: Preview with WhatIf, confirm where it matters, use the smallest permission that works, read before you run, and audit by re-reading the setting and checking events. A preview checks the target before the change. A confirmation prompt gives you a pause at the risky step. A re-read setting and a matching event together give you evidence that the change you intended is the change that actually happened.
Next step: From functions to a tool