Finding PowerShell Default Variables (Part 1)

Sometimes it would be useful to identify the automatic PowerShell variables managed by PowerShell so you could differentiate between built-in variables and your own. Get-Variable always dumps all variables.

Here is a simple trick that uses a separate new and fresh PowerShell runspace to determine the built-in PowerShell variables:

# create a new PowerShell
$ps = [PowerShell]::Create()
# get all variables inside of it
$null = $ps.AddCommand('Get-Variable')
$result = $ps.Invoke()
# dispose new PowerShell
$ps.Runspace.Close()
$ps.Dispose()

# check results
$varCount = $result.Count
Write-Warning "Found $varCount variables."
$result | Out-GridView

When you run this, the code reports the number of found variables, and the variables.

Twitter This Tip! ReTweet this Tip!

Finding PowerShell Classes

Starting in PowerShell 5, you can define PowerShell classes. They are defined dynamically and live in memory. So how would you know the names of classes that have been defined?

Let’s first define a really simple class that does not do anything:

class TestClass
{
    
}

How would you be able to check whether a class named “TestClass” exists in memory? Here is a helper function called Get-PSClass:

function Get-PSClass($Name = '*')
{
  [AppDomain]::CurrentDomain.GetAssemblies() | 
  Where-Object { $_.GetCustomAttributes($false) | 
      Where-Object { $_ -is [System.Management.Automation.DynamicClassImplementationAssemblyAttribute]} } | 
      ForEach-Object { $_.GetTypes() | 
      Where-Object IsPublic |
      Where-Object { $_.Name -like $Name } |
      Select-Object -ExpandProperty Name
  }
}

When you run it, it reports the names of all currently defined PowerShell classes in memory (in our PowerShell example, there were a couple of other PowerShell classes found from previous experiments as well):

 
PS> Get-PSClass
HelperStuff
Employee
TestClass

PS>  
 

You can also explicitly test for a class name:

 
PS> Get-PSClass -Name TestClass
TestClass

PS> (Get-PSClass -Name TestClass) -ne $null 
True

PS> (Get-PSClass -Name TestClassNotExisting) -ne $null 
False
 

And you can use wildcards. This would report any class starting with an “A” through “H”:

 
PS> Get-PSClass -Name '[A-H]*'
HelperStuff
Employee
 

Twitter This Tip! ReTweet this Tip!

Using PowerShell Classes 2

Starting in PowerShell 5, you can define PowerShell classes. You can use classes to create new objects, and by defining one or more “constructors”, you can easily initialize the newly created objects as well.

Let’s have a look:

class Employee
{
    [int]$Id
    [string]$Name

    Employee([int]$Id, [string]$Name)
    {
        $this.Id = $Id
        $this.Name = $Name
    }
    Employee ([string]$Name)
    {
        $this.Id = -1
        $this.Name = $Name
    }
    Employee ()
    {
        $this.Id = -1
        $this.Name = 'Undefined'
    }
}

Once you run this code, there is a new class called “Employee” with three constructors. And here is how you can use the new class:

 
PS> [Employee]::new()

Id Name     
-- ----     
-1 Undefined



PS> [Employee]::new('Tobias')

Id Name  
-- ----  
-1 Tobias



PS> [Employee]::new(999, 'Tobias')

 Id Name  
 -- ----  
999 Tobias



PS> 
 

Each call is using a different constructor, and the class creates and initializes new objects for you accordingly.

Twitter This Tip! ReTweet this Tip!