My PowerShell

In this article I show how to collect some information of interest on the environment you're working in.
You get to learn about your host, your profile, modules, execution policy and the likes.

My PowerShell

When using PowerShell, it may be necessary or at least useful to have some information on the tool you're working with.
The success of a script or command can depend on the PowerShell version you're using, or the content of your profile and so on. I'll show you some useful commands right away:

The Host

Actually, what we usually call PowerShell is actually the PowerShell Host or Console. PowerShell itself is the engine and invisible to us. Besides the Console there are other hosts, like Windows PowerShell ISE Host or Visual Studio Code Host

You can probably guess what cmdlet will get you the information on the host you're in:

PS> Get-Host

You get to see the name of the host, and its version.
Get-Host

We can also see all PowerShell Hosts that are currently running:

PS> Get-PSHostProcessInfo

Get-PSHostProcessInfo

My version

The version and edition of the actual PowerShell Engine is available in a variable:

PS> $PSVersionTable

PSVersionTable

My ExecutionPolicy

You may already know

PS> Get-ExecutionPolicy

but that's only part of the story. Actually there are 5 execution policies, and you get to see them like this:

PS> Get-ExecutionPolicy -List

Get-ExecutionPolicy

Tobias Weltner explains it all in detail: Understanding Execution Policy

My Profile

When running PowerShell from a host, there may be 4 profiles loaded. The paths are defined by the user account and the host. We get a list of the profile paths with:

PS> $PROFILE | Select-Object *

Profile

We see 4 paths listed, but that does not mean the files physically exist. We can check them ( and if wanted, open them ) with this function:

function Get-MyProfile {
  <#
    .SYNOPSIS
    Shows all profile files.

    .DESCRIPTION
    The four potential file locations for the profile are returned, together with an extra property for the files that really exist.

    The $PROFILE variable holds 4 filepaths, depending on the current user and the current host. That does not mean those files all exist.
    This function returns not only the provided paths, but also whether they really exist.

    .PARAMETER Edit
    When this switch is added, the existing profile files are opened with notepad.

    .EXAMPLE
    Get-MyProfile -Edit
    Shows what locations are provided for the four profile files for the current context, and the actual files for those that exist.
    The existing files are opened with notepad.

    .NOTES
    Author: Klaas Vandenberghe
    Date:   2017-03-09

    .INPUTS
    None

    .OUTPUTS
    PSCustomObject
    Files
  #>

  [CmdletBinding()]
  Param (
    [Switch]$Edit
  )

  if ($PSBoundParameters.ContainsKey('Edit'))
  {
    if ( Test-Path -Path $profile.AllUsersAllHosts ) { & "$env:windir\system32\notepad.exe" $profile.AllUsersAllHosts }
    if ( Test-Path -Path $profile.AllUsersCurrentHost ) { & "$env:windir\system32\notepad.exe" $profile.AllUsersCurrentHost }
    if ( Test-Path -Path $profile.CurrentUserAllHosts ) { & "$env:windir\system32\notepad.exe" $profile.CurrentUserAllHosts }
    if ( Test-Path -Path $profile.CurrentUserCurrentHost ) { & "$env:windir\system32\notepad.exe" $profile.CurrentUserCurrentHost } 
  }

  [PSCustomObject]@{
        AllUsersAllHosts = $profile.AllUsersAllHosts
        AllUsersCurrentHost = $profile.AllUsersCurrentHost
        CurrentUserAllHosts = $profile.CurrentUserAllHosts
        CurrentUserCurrentHost = $profile.CurrentUserCurrentHost
        AllUsersAllHostsFile = $(if ( Test-Path -Path $profile.AllUsersAllHosts ) { $profile.AllUsersAllHosts })
        AllUsersCurrentHostFile = $(if ( Test-Path -Path $profile.AllUsersCurrentHost ) { $profile.AllUsersCurrentHost })
        CurrentUserAllHostsFile = $(if ( Test-Path -Path $profile.CurrentUserAllHosts ) { $profile.CurrentUserAllHosts})
        CurrentUserCurrentHostFile = $(if ( Test-Path -Path $profile.CurrentUserCurrentHost ) { $profile.CurrentUserCurrentHost})
  }
}

Get-MyProfile

My variables

Sometimes you forget the variables you declared in your session or can't remember what the name of a certain system variable is. List them with:

PS> Get-Variable

Check out

PS> help Get-Variable -ShowWindow

and find what options you have when you want them filtered, or you need to see more properties like the description, the module or visibility,...

My modules

Modules can be anywhere. However, they can only be autoloaded if they are kept in one of the directories listed in the environment variable PSModulePath ( and if your PowerShell version is 3 or higher ). The order in which those directories are listed also dictates the order of discovery. In other words: if you have modules with the same name, the one PowerShell bumps into first, will be loaded.

PS> $env:PSModulePath -split ';'

ModulePath

We can also take a look at the modules that are available versus the modules that are already loaded into our session. If we want to know where they are, we can show their path too:

PS> Get-Module -ListAvailable
PS> Get-Module
PS> Get-Module | Select-Object Path

My path

We get the directories in our path the same way we got our module paths:

PS> $env:Path -split ';'

Previous Post Next Post