Check my variables function

Posted: March 30, 2016 in PowerShell scripts


If you are familiar with Powershell scripting, you surely know that variables can really mess up your day.

I’ve written a little function you can dot source into your script and run at specific points in your script to show the current variables and their types and values. It’s not perfect, and I’ll probably make it prettier in time. But here you go.



 Function CheckMyVariables

    Function CheckVariableType

            Write-Host ""
            Write-Host -ForegroundColor DarkGray "*Started*"

            #Check what kind of type the variable is and if it is empty
            If(!($InputData)){Write-Host -ForegroundColor Yellow "The variable $Inputname is Null or Empty ";Return}
            If($InputData -is [String] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is a: " -NoNewline;Write-Host "[String]"}
            Elseif ($InputData -is [Int] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Int]"}
            Elseif ($InputData -is [Array] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Array]"}
            Elseif ($InputData -is [System.Boolean] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Boolean]"}
            Elseif ($InputData -is [System.Collections.Arraylist] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Arraylist]"}

            #Formatting output of Arrays and Arraylists
            If($InputData -is [Array] -eq $true -or $InputData -is [System.Collections.Arraylist] -eq $true)
                Write-Host -ForegroundColor Green ""$"$Inputname value(s) is/are:"

            #Normal formatting on everything else
                Write-Host -ForegroundColor Green ""$"$Inputname value(s) is/are: " -NoNewline
                Write-Host $InputData

            #If switch is enabled then run the following
            If($Full -eq $true)
                Write-Host -ForegroundColor Green ""$"$Inputname type confirmed with .GetType() cmlet: " -NoNewline
                Write-Host $InputData.GetType()

            Write-Host -ForegroundColor DarkGray "*Finished*"

    #A function to remove multiple values from an array
    Function RemoveArrayValues ($Array,$RemoveValues)
        Foreach ($value in $RemoveValues)
            $Array = $Array | ? {$_ -ne $value}
        Return $Array

    #Here I list environment variables and other variables i want to exclude from view..
    $ExcludeVar = '$','?','^','args','ConfirmPreference','ConsoleFileName','DebugPreference','Error','ErrorActionPreference','ErrorView','ExecutionContext','false','FormatEnumerationLimit',

    #Get all variables
    $allVariables = (Get-Variable).Name

    #Remove excludevariableslist
    $allVariables = RemoveArrayValues $allVariables $ExcludeVar
    Foreach ($var in $allVariables)
        $checkvar = Get-Variable -Name $var
        CheckVariableType $checkvar.Value $var

    #Default value of the switch is false
    $Full = $false

Run as follows


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s