Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Removing Obsolete PowerShell Remoting Configurations

Posted on October 20, 2021October 20, 2021

I'm pretty sure I've discussed this before, but Microsoft is scheduled to release PowerShell 7.2 soon, I thought it might be good to revisit this topic. Here's the potential issue. If you've been installing PowerShell 7 releases for a while, and have been enabling PowerShell remoting, you most likely have a list of remoting session configurations like this.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

If you run Get-PSSessionConfiguration in Windows PowerShell, you'll these in addition to the Windows PowerShell 5.1 sessions. Now, there may not be any problem in having all of these configurations, but personally, I find it a bit messy. I don't like keeping something if it isn't being used. Time for a little housekeeping.

Unregister-PSSessionConfiguration

Removing a session configuration is simple enough using Unregister-PSSessionConfiguration. Specify the configuration name, and it is gone. The cmdlet also supports -WhatIf and -Confirm which I always recommend that you use. This is equivalent to the carpenter's adage of "Measure twice, cut once." But I want an easy way to dump everything I'm not using except for the session configuration associated with the current PowerShell version. On my system that is 7.1.5. As an added twist, I also have the latest preview of PowerShell 7.2 installed, including a remoting configuration. I'd like to keep that as well. I know over time I'll need to do this housekeeping task again, so I might as well build a PowerShell function.

Remove-ObsoleteSessionConfiguration

My function is written to run in Windows PowerShell 5.1 as well as PowerShell 7. If I'm running in Windows PowerShell, I want to exclude the default session configurations. I'll use a regular expression pattern.

$Exclude = "microsoft.powershell|microsoft.powershell.workflow|microsoft.powershell32|microsoft.windows.servermanagerworkflows|PowerShell.$($PSVersionTable.PSVersion)"

The last entry is calculated and should correspond to a PowerShell 7 session configuration. When I run this in Windows PowerShell this will create an entry that will likely never exist, but that won't affect the filtering.

 (Get-PSSessionConfiguration -verbose:$False).where({$_.name -notmatch $exclude})

I'm using the Where() method to gain a slight performance advantage. By the way, I am intentionally turning off Verbose output for Get-PSSessionConfiguration. The output for this command shows functions that are going to be run and very little in the way of useful information. I suspect the verbose output was used during development, which is a good technique, but then the verbose output was never revised.

Each session configuration is then piped to Unregister-PSSessionConfiguration.

 Unregister-PSSessionConfiguration -Verbose:$false -Confirm

Again, I'm turning off Verbose output for the same reason. I am also explicitly using the -Confirm parameter. I want to always be prompted to remove the session configuration.

My function includes support for -Whatif.

Of course, when I run the function without -Whatif, the confirm process takes over.

I can select the configurations to remove and those, like the PowerShell preview, that I want to keep.

Try It

Here is the code I am using. I also encourage you to read the about_Functions_CmdletBindingAttribute help topic.

#requires -version 5.1
#requires -RunAsAdministrator

Function Remove-ObsoleteSessionConfiguration {
    [cmdletbinding(SupportsShouldProcess,ConfirmImpact="High")]
    Param()
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)"
        #exclude these default configurations as a regex pattern
        $Exclude = "microsoft.powershell|microsoft.powershell.workflow|microsoft.powershell32|microsoft.windows.servermanagerworkflows|PowerShell.$($PSVersionTable.PSVersion)"
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Excluding $Exclude"
    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting non-default PowerShell Remoting configurations"
        <#
         turn off Verbose output for Get-PSSessionConfiguration and Unregister-PSSessionConfiguration.
         These command displays PowerShell code which adds no value for my purpose.
        #>
        (Get-PSSessionConfiguration -verbose:$False).where({$_.name -notmatch $exclude}) |
        Unregister-PSSessionConfiguration -Verbose:$false -Confirm
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
    } #end

} #close Remove-PSRemotingConfiguration

Use with great caution and test it thoroughly in a non-production environment, especially if you have created your own custom session configurations.

If you have custom configurations, make sure you have all the required files to re-create them if necessary. After running this function, you should restart with WinRM service.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d