Getting all desktosp and servers powershell version

As you all are aware about the security notification that MS release about the Powershell version to be updated to due to security flaw. I was planning to get the different version of Powershell on our desktops and servers. So, I wrote this script and wanted to know whether this would work before I run it on my production environment?

$adcomputer=(Get-ADComputer -Filter *).Name
foreach ($computer in $adcomputer)
Invoke-Command -ComputerName $adcomputer -Scriptblock {$PSVersionTable.psversion} -ErrorAction SilentlyContinue | out-file c:\result.txt -append -force

Welcome to the forum.

Are we? If I’m not wrong the Windows PowerShell gets updated with the OS.

Since you do not do something harmful in your snippet it schould be quite safe/harmless to run the code even on your production environment.

Since the parameter -ComputerName from the cmdlet Invoke-Command can take a list of computers you actually don’t need a loop for your task.

$adcomputerList = (Get-ADComputer -Filter *).Name
Invoke-Command -ComputerName $adcomputerList  -Scriptblock { $PSVersionTable.PSVersion } -ErrorAction SilentlyContinue | 
    Select-Object -Property PSComputerName,Major,Minor,Build,Revision |
        Export-CSV -Path c:\result.csv -NoTypeInformation -Delimiter ','

While PS does normally update with the OS, I believe the OP is referencing the alert a few days ago in which Microsoft is strongly suggesting everyone upgrade now:

While the alert seems geared toward Azure, the flaw itself is in .Net, and is thus an issue for everyone.

@oryx360 I don’t see why that script would not work, so long as the computers are actually up and available at the time you run it.

This is the error I get

Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.
At C:\Users\Usr001\Documents\Powershell Scripts\PowershellVersion1.ps1:2 char:1

  • Invoke-Command -ComputerName $adcomputerList -Scriptblock { $PSVersi …
  •   + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
      + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

So you should check the list of computer names you create with your AD query.