Windows Server · Lesson 17

PowerShell for Server Administration

Use PowerShell for defensive inventory, evidence collection, filtering, verification, and carefully controlled Windows Server administration.

PowerShell is most valuable when it helps you understand the server before you change it. Read-only inspection should come before broad automation.

Learning objectives

01

Use PowerShell as a defensive Windows Server administration and verification tool.

02

Differentiate inspection commands from high-impact change commands.

03

Build repeatable read-only inventories for roles, services, users, groups, shares, firewall, Defender, tasks, logs, DNS, and DHCP.

04

Filter and format PowerShell output so evidence is easier to interpret.

05

Use PowerShell carefully on member servers and domain controllers without assuming every cmdlet applies everywhere.

06

Verify every administrative change with both PowerShell and the required server function.

PowerShell mindset

Prefer Get, Test, Resolve, Select, Where, and Sort while investigating.
Treat Set, New, Add, Remove, Disable, Enable, Stop, and Restart as change operations that require a plan.
Record the original state before you change it.
Change one object or setting at a time when possible.
Verify the configuration and the real server function afterward.

Core inspection commands

Get-ComputerInfo
Get-WindowsFeature
Get-Service
Get-CimInstance Win32_Service
Get-LocalGroupMember
Get-SmbShare
Get-NetFirewallProfile
Get-MpComputerStatus
Get-ScheduledTask
Get-WinEvent

Role-specific commands

Active Directory: Get-ADUser, Get-ADGroupMember
DNS: Get-DnsServerZone, Get-DnsServerResourceRecord
DHCP: Get-DhcpServerv4Scope, Get-DhcpServerv4Lease
File server: Get-SmbShareAccess, Get-Acl
Remote administration: Test-WSMan
Windows Update history: Get-HotFix

Read-only baseline

Inspect before changing

Installed roles

Get-WindowsFeature | Where-Object {$_.Installed -eq $true} | Select-Object DisplayName, Name, InstallState

Shows installed Windows Server roles and features.

Detailed services

Get-CimInstance Win32_Service | Select-Object Name, DisplayName, State, StartMode, StartName, PathName

Shows service state, startup mode, account, and executable path.

Firewall profiles

Get-NetFirewallProfile | Select-Object Name, Enabled, DefaultInboundAction, DefaultOutboundAction

Shows firewall profile state and default actions.

Defender status

Get-MpComputerStatus

Shows Defender health and protection state.

Scheduled tasks

Get-ScheduledTask | Select-Object TaskPath, TaskName, State

Creates a task inventory.

Recent System events

Get-WinEvent -LogName System -MaxEvents 30 | Select-Object TimeCreated, Id, ProviderName, LevelDisplayName, Message

Shows recent operating-system events.

Safer change patterns

Inspect first

Get-Service -Name "ExampleService"

Confirm the current state before changing a service.

Use WhatIf when supported

Remove-ADGroupMember -Identity "Domain Admins" -Members "exampleuser" -WhatIf

Preview supported changes without applying them.

Verify immediately

Get-ScheduledTask -TaskName "ExampleTask" | Select-Object TaskName, State

Confirm the expected result before moving on.

Deeper administration review

Use PowerShell to answer a specific server question

Service investigation

Use Get-Service for state and Get-CimInstance Win32_Service for StartMode, StartName, and PathName.
Use -RequiredServices and -DependentServices before stopping or disabling anything.
Compare service paths with installed software and the server role.
Use Service Control Manager events to correlate unexpected starts, stops, and installations.

Identity investigation

Use Get-LocalUser and Get-LocalGroupMember on applicable member servers.
Use Get-ADUser, Get-ADGroupMember, and Get-ADPrincipalGroupMembership for domain identities.
Do not treat old activity or unfamiliar names alone as proof an account is unnecessary.
Verify service, scheduled-task, application, and role dependencies before changing an identity.

Network exposure

Use Get-NetFirewallProfile to understand profile state before reviewing individual rules.
Use Get-NetFirewallRule with associated port, address, application, and service filters.
Use Get-NetTCPConnection to compare listening services with intended server roles.
Narrow required exposure rather than disabling required services blindly.

Evidence collection

Use Get-Date to record the collection time.
Export selected objects to CSV when a before/after comparison is useful.
Use Get-WinEvent with a relevant time window instead of collecting massive unfiltered logs.
Preserve evidence before remediation when suspicious tasks, services, accounts, or files are involved.

Decision cases

PowerShell decisions in server context

Downloaded hardening script

The script disables many services, removes accounts, changes firewall rules, and edits security settings in one run.

The commands may be defensive in isolation, but the script does not understand the current server role or scenario dependencies.

Response

Do not execute it as a block. Convert the script into an inspection checklist, verify each target, then apply only justified changes one at a time.

Stopped service looks unnecessary

Get-Service shows the service as Stopped and its display name is unfamiliar.

Demand-start and trigger-start services can be legitimate, especially on Windows Server.

Response

Inspect startup mode, executable path, required/dependent services, server-role ownership, and recent events before deciding whether to change it.

Firewall rule name looks safe

A rule has a trusted-looking display name, but the actual remote scope and profiles have not been reviewed.

Display names do not prove the rule is narrowly configured.

Response

Inspect the rule plus its port, address, service, application, profile, and direction filters before changing it.

Local-group command fails on a DC

A local account-management cmdlet does not behave as expected on a domain controller.

A domain controller uses Active Directory identities rather than the normal member-server local account model.

Response

Switch to AD-aware tools and cmdlets rather than forcing the local-account workflow.

Troubleshooting

When PowerShell does not behave as expected

Situation

Cmdlet not recognized

Check spelling, then use Get-Command and Get-Module -ListAvailable. The required management module may not be installed.

Situation

Access denied

Confirm whether the operation truly needs elevation. Reopen PowerShell as administrator only if the task requires it.

Situation

Output is overwhelming

Filter early with Where-Object, select only relevant properties, limit event time ranges, and sort results.

Situation

Setting changes but later reverts

Investigate Group Policy, scheduled tasks, management agents, domain policy, replication, or application self-repair.

Situation

Command succeeds but the role breaks

Use the before-state, Event Viewer, dependency information, and client-side verification to identify which change affected the required service.

Situation

GUI and PowerShell disagree

Confirm you are viewing the same server, same policy scope, same firewall profile, same account context, and the effective rather than merely local setting.

Fictional defensive lab

Review APP-SRV with PowerShell

Step 01

Collect a baseline

On fictional APP-SRV, record OS/build, installed roles, services, Administrators membership, shares, firewall profiles, Defender status, scheduled tasks, and recent System events.

Step 02

Investigate a service

Inspect one unfamiliar service using Get-Service, Win32_Service, dependency information, and Service Control Manager events.

Step 03

Investigate a scheduled task

Review the task principal, trigger, action path, state, and last-run information without executing it manually.

Step 04

Review exposure

Inspect RDP or WinRM firewall rules and compare them with the management path required by the scenario.

Step 05

Make one controlled change

Choose a clearly unnecessary or misconfigured item, record the original state, use the narrowest supported change, and avoid broad pipelines.

Step 06

Verify the real outcome

Re-run the relevant PowerShell checks, review fresh events, and test the required application, share, role, or remote-management function.

Common mistakes

01

Running copied hardening scripts without reading each command.

02

Using elevated PowerShell for every task even when it is not required.

03

Using broad modification pipelines that can affect many objects.

04

Assuming a successful cmdlet means the server role is healthy.

05

Ignoring module availability and server-role differences.

06

Skipping a before-state baseline.

Verification checklist

01

The intended configuration change is visible in PowerShell.

02

The GUI or effective policy is consistent when applicable.

03

Required services are healthy.

04

Required roles remain installed.

05

Required accounts and privileges remain intact.

06

Required firewall, Defender, share, task, DNS, and DHCP settings still work.

07

No unexpected new Event Viewer errors appeared.

08

Client-side tests still succeed for required network services.

09

Before/after evidence is documented.

10

No unrelated objects were changed.

Reflection

Question 1

Why should read-only commands usually come before change commands?

Question 2

What does WhatIf help you understand?

Question 3

Why can a command behave differently on a member server and a domain controller?

Question 4

Why is a successful PowerShell command not enough to prove the server is healthy?

Question 5

What should you record before running a high-impact command?

Question 6

Why are PowerShell objects more useful than plain text output?

Final Windows Server lesson

Windows Server Final Review Checklist

Next, combine identity, policy, Defender, firewall, patching, roles, shares, remote administration, DNS, DHCP, tasks, logs, and PowerShell into one final server verification workflow.

Open Lesson 18 →