Search code examples
powershellpowershell-cmdlet

What PowerShell version introduced a given cmdlet?


I'm putting #Requires -Version at the top of my script, but need to figure out what version I require. I was hoping to query what version of PowerShell introduced each of the cmdlets I call. But I don't see that in the Get-Help -verbose output for one of the cmdlets. I didn't find a canonical web page listing for it.

Anyone know if there's a standard way to look up what version of PowerShell introduced a particular cmdlet? Or, is there a better way to accomplish what I'm trying to do?


Solution

  • So, as far as I am aware the "standard" way to look this up is to read MSDN. :-) You can get to the relevant page pretty easily using the -Online switch for Get-Help, for example:

    Get-Help -Name "Get-DscConfiguration" -Online
    

    Another approach might be to fire up powershell.exe using the -Version switch to set a specific version, e.g. powershell.exe -Version 2 then use the Get-Command cmdlet to see if your cmdlets are listed or not.


    I have enjoyed myself! Here is some code that seems to work OK parsing a script and then working out if the commands are valid cmdlets in different PS versions. At this point, it doesn't seem that "1.0" or "5.0" are supported by the -PSVersion switch on Start-Job though.

    param(
      $file = 'C:\scripts\PowerShell\Toolkit\Get-PSVersionCompatibility.ps1'
    )
    
    New-Variable tokens
    New-Variable parseerrors
    $p = [System.Management.Automation.Language.Parser]::ParseFile($file,[ref]$tokens,[ref]$parseerrors)
    $Commands = $tokens | ?{$_.TokenFlags -contains "CommandName"} | Sort -Unique | Select Value
    
    $ScriptBlock = {
      param($PSVersion,$Commands)
    
      $Output = New-Object -TypeName PSObject -Property @{PSVersion = $PSVersion}
    
    
      foreach($Command in $Commands) {
        if([String]::IsNullOrEmpty($Command.Value)){continue}
    
        if(Get-Command | ?{$_.Name -eq $Command.Value}) {
          $Available = $true
        } else {
          $Available = $false
        }
    
        $Output | Add-Member -MemberType NoteProperty -Name $($Command.Value) -Value $Available
      }
    
      return $Output
    }
    
    $Results = @()
    
    foreach($PSVersion in 2..4) {
      $job = Start-Job -PSVersion "$PSVersion.0" -ScriptBlock $ScriptBlock -ArgumentList $PSVersion,$Commands
    
      Wait-Job $job | Out-Null
      $Results += (Receive-Job $job | Select PSVersion,*-*)
      Remove-Job $job
    }
    
    $Results | FT -AutoSize
    
    Remove-Variable tokens
    Remove-Variable parseerrors