Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Powershell Switch-Statement doesn't change value

This is the Powershell script I wrote:

$varCompList = Get-ADComputer -Filter "Name -like '*Name of Computers*'" -Properties OperatingSystemVersion | select DNSHostName, OperatingSystemVersion
foreach ($System in $varCompList){
    $Restult=switch ($System.OperatingSystemVersion){
        "10.0 (20348)"{"Server 2022"}
        "10.0 (19042)"{"Server 2019 20H2"}
        "10.0 (18363)"{"Server 2019 1909"}
        "10.0 (17763)"{"Server 2019 1809"}
        "10.0 (14393)"{"Server 2016"}
    }
}
echo $varCompList

It displays all the Servers like it should but the OperatingSystemVersion is still displayed as 10.0 (14393).

What am I missing?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're currently assigning the mapped OS names to a variable, but you never use it for anything and you never update the original input object.

Instead of assigning the result to a variable, assign it to the OperatingSystemVersion property on each object instead:

foreach ($System in $varCompList){
    $System.OperatingSystemVersion = switch ($System.OperatingSystemVersion){
        "10.0 (20348)"{"Server 2022"}
        "10.0 (19042)"{"Server 2019 20H2"}
        "10.0 (18363)"{"Server 2019 1909"}
        "10.0 (17763)"{"Server 2019 1809"}
        "10.0 (14393)"{"Server 2016"}
        default { $_ }
    }
}

The default case will ensure you preserve the original version string for any computer that doesn't have any of the listed versions installed.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!