RousselTMLEARNING

TP · Windows administration: from discovery to expertise

Lab: Inventory a server with PowerShell with correction

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

An Inventaire.ps1 script, a CSV report and an HTML page.

Setup #

Guided steps, with their solution.

  1. 1. Prepare the workspace

    Why? A dedicated folder avoids mixing scripts and reports.

    Create C:\TP\Scripts and C:\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 -List
  2. 2. 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. 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, Status
  4. 4. Write the script

    Why? A parameterized script can be reused and run remotely.

    Write C:\TP\Scripts\Inventaire.ps1 returning 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.ps1
  5. 5. Produce the reports

    Why? CSV opens in a spreadsheet, HTML can be shared.

    Export the result as timestamped CSV and HTML in C:\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\Rapports
  6. 6. Run remotely

    Why? Invoke-Command queries several servers in parallel.

    Run the script on the local server and a second machine (or localhost twice 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

  • ⏱ Estimated time : ≈ 45 min

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.

Ask a question about this lesson →

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...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!