The correction is integrated into the TP (See the green boxes)
What you will learn in this TP
- Discover cmdlets with Get-Command and Get-Member
- Filter, sort and export objects
- Write a parameterized script and run it remotely
Introduction #
What you will learn in this section
- Discover cmdlets with Get-Command and Get-Member
- Filter, sort and export objects
- Write a parameterized script and run it remotely
Context. Your manager asks for a server status report before a migration. Rather than clicking through each console, you write a reusable inventory script.
Prerequisites: a disposable Windows Server 2025 virtual machine (free evaluation) with an administrator account, and PowerShell open as administrator · Estimated duration: 45 min
AnInventaire.ps1 script, a CSV report and an HTML page.Setup #
Guided steps, with their solution.
1. Prepare the workspace
Why? A dedicated folder avoids mixing scripts and reports.
CreateC:\TP\ScriptsandC:\TP\Rapports, check the PowerShell version and allow local scripts.See the correction
New-Item -ItemType Directory C:\TP\Scripts, C:\TP\Rapports -Force $PSVersionTable.PSVersion Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Force Get-ExecutionPolicy -List2. Explore the system
Why? Get-Member reveals available properties before selecting them.
Display the server’s name, version, build, total memory and last boot.See the correction
Get-CimInstance Win32_OperatingSystem | Get-Member -MemberType Property | Select-Object -First 15 $os = Get-CimInstance Win32_OperatingSystem $cs = Get-CimInstance Win32_ComputerSystem [pscustomobject]@{ Nom = $env:COMPUTERNAME Systeme = $os.Caption Build = $os.BuildNumber RAM_Go = [math]::Round($cs.TotalPhysicalMemory / 1GB, 1) Demarrage = $os.LastBootUpTime }3. Disks and services
Why? The two most frequent incident causes: full disk and stopped service.
List volumes with their free percentage, then automatic services that are not running.See the correction
Get-Volume | Where-Object DriveLetter | Select-Object DriveLetter, FileSystemLabel, @{n='Taille_Go'; e={[math]::Round($_.Size/1GB,1)}}, @{n='Libre_pct'; e={[math]::Round(100*$_.SizeRemaining/$_.Size,1)}} Get-Service | Where-Object { $_.StartType -eq 'Automatic' -and $_.Status -ne 'Running' } | Select-Object Name, DisplayName, Status4. Write the script
Why? A parameterized script can be reused and run remotely.
WriteC:\TP\Scripts\Inventaire.ps1returning one object per server (system, RAM, free C disk, number of stopped automatic services, latest update).See the correction
@' [CmdletBinding()] param() $os = Get-CimInstance Win32_OperatingSystem $c = Get-Volume -DriveLetter C [pscustomobject]@{ Serveur = $env:COMPUTERNAME Systeme = $os.Caption RAM_Go = [math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory/1GB,1) C_Libre_pct = [math]::Round(100*$c.SizeRemaining/$c.Size,1) ServicesArretes = (Get-Service | Where-Object { $_.StartType -eq 'Automatic' -and $_.Status -ne 'Running' }).Count DernierCorrectif = (Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 1).HotFixID } '@ | Set-Content C:\TP\Scripts\Inventaire.ps1 -Encoding UTF8 & C:\TP\Scripts\Inventaire.ps15. Produce the reports
Why? CSV opens in a spreadsheet, HTML can be shared.
Export the result as timestamped CSV and HTML inC:\TP\Rapports.See the correction
$stamp = Get-Date -Format 'yyyy-MM-dd_HHmm' $inv = & C:\TP\Scripts\Inventaire.ps1 $inv | Export-Csv "C:\TP\Rapports\inventaire_$stamp.csv" -NoTypeInformation -Encoding UTF8 $inv | ConvertTo-Html -Title 'Inventaire' -PreContent "<h1>Inventaire du $stamp</h1>" | Set-Content "C:\TP\Rapports\inventaire_$stamp.html" -Encoding UTF8 Get-ChildItem C:\TP\Rapports6. Run remotely
Why? Invoke-Command queries several servers in parallel.
Run the script on the local server and a second machine (orlocalhosttwice if you only have one), then gather the results.See the correction
Enable-PSRemoting -Force $cibles = 'localhost', $env:COMPUTERNAME Invoke-Command -ComputerName $cibles -FilePath C:\TP\Scripts\Inventaire.ps1 | Select-Object * -ExcludeProperty RunspaceId | Sort-Object C_Libre_pct | Format-Table -AutoSize
Difficulty level : ●○○○○ (1/5)
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
PowerShell for Windows administrators
Master PowerShell to administer Windows: cmdlets and help, object pipeline, filters and formats, ...
Windows and Windows Server: versions, editions and architecture
Discover the Windows ecosystem for administrators: Windows 11 and Windows Server 2025, editions a...
Windows storage: disks, volumes, Storage Spaces and RAID
Manage storage on Windows: GPT and MBR, partitions and volumes with PowerShell, NTFS and ReFS, St...
Windows updates and security: WSUS, Defender, BitLocker, LAPS and hardening
Maintain and secure a Windows estate: update strategy (Windows Update, WSUS, Azure Update Manager...
Active Directory Domain Services and DNS
Understand and deploy Active Directory: forest, domains, organizational units, domain controllers...
Services, scheduled tasks, event logs and performance
Control Windows services and their recovery, automate with Task Scheduler, use event logs with Ge...
Backup, Hyper-V and Windows troubleshooting
Back up and restore with Windows Server Backup (including system state and Active Directory), vir...
Windows networking: IP, DNS, DHCP, firewall and remote access
Configure and troubleshoot Windows networking with PowerShell, deploy a DHCP server, master Windo...
Windows administration best practices
The production checklist for Windows and Windows Server: installation, identity and Active Direct...