In PowerShell versions less than v5 you'll notice that the friendly Write-Information cmdlet doesn't exist. That's fine, until you write a PowerShell script that uses Write-Information (or any other command that doesn't exist).

I set out on my journey of writing a script that would run on multiple servers, on multiple OS's and found that there several cmdlets that exist on newer versions of PowerShell that don't exist on the older versions.

With this problem, I needed a way of checking to see if a cmdlet existed, and if not, create one with at least similar functionality.

To my amazement you can define a function inside an if statement and it will work in the rest of your script or powershell session. So, here is what I did to get Write-Information to work.

if ((Get-Command "Write-Information" -ErrorAction SilentlyContinue) -eq $null) {
    function Write-Information($MessageData) {
        Write-Host $MessageData
    }
}

The way I use Write-Information this is all I needed to make my script work on 2008R2 and 2012R2.

I tested this with a new powershell window and ran this:

$InformationPreference = "Continue"
Write-Information negative

if ((Get-Command "Write-Information" -ErrorAction SilentlyContinue) -eq $null) {
    write-host "Write-Information does not exist"
    function Write-Information($MessageData) {
        Write-Host $MessageData
    }
}

Write-Information test

2008R2/2012R2 Results (negative is not displayed and instead an exception is, but Write-Information does not exist does. The second call to write-information worked as expected.

Write-Information : The term 'Write-Information' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Write-Information negative
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Write-Information:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Write-Information does not exist
test

2016/2019 Results, both commands worked without creating a new Write-Information method:

negative
test