Checking Cmdlet Availability and Script Compatibility (Part 1)

by Mar 12, 2019

Not all PowerShell cmdlets ship with PowerShell. Many cmdlets are part of 3rd party modules which in turn ship when you install certain software, or use certain Windows versions.

To find out the compatibility status of your scripts, in our first part we’re looking at ways to find out which commands a script actually uses. Here is a helper function that utilizes the internal PowerShell Abstract Syntax Tree (AST) to identify commands:

function Get-ContainedCommand


    $Token = $Err = $null
    $ast = [Management.Automation.Language.Parser]::ParseFile($Path, [ref] $Token, [ref] $Err)

    $ast.FindAll({ $args[0].GetType().Name -eq "${ItemType}Ast" }, $true) 

Get-ContainedCommand can extract either the functions defined in a script, or the commands used by a script. Here is the code to dump all the functions defined in a script:

$Path = "C:\scriptToPS1File\WithFunctionDefinitionsInIt.ps1"

$functionNames = Get-ContainedCommand $Path -ItemType FunctionDefinition | 
  Select-Object -ExpandProperty Name


This is the list of commands used internally by the script:

$commands = Get-ContainedCommand $Path -ItemType Command 

To identify the use of external commands, simply subtract all internally defined functions from the command list, and eliminate duplicates. This gets you the list of external commands used by the script:

$Path = "C:\scriptToPS1File\WithFunctionDefinitionsInIt.ps1"

$functionNames = Get-ContainedCommand $Path -ItemType FunctionDefinition | 
  Select-Object -ExpandProperty Name

$commands = Get-ContainedCommand $Path -ItemType Command 

$externalCommands = $commands | Where-Object {
      $commandName = $_.CommandElements[0].Extent.Text
      $commandName -notin $functionNames
  } | 
  Sort-Object -Property { $_.GetCommandName() } -Unique – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!