Toolkit Extension: Remove-Path($Path) : Removes $Path from the %Path% Variable

Topics: Archive - Toolkit Extensions
Nov 20, 2013 at 10:48 PM
# Remove-Path Function:  Removes folder from Path System Variable
    Function global:Remove-Path()
        {
            [Cmdletbinding()]
            param
            ( 
                [parameter(Mandatory=$True,
                ValueFromPipeline=$True,
                Position=0)]
                [String[]]$RemovedFolder
            )

        # Get the Current Search Path from the environment keys in the registry
            $NewPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

        # Find the value to remove, replace it with $NULL. If it’s not found, nothing will change.
            $NewPath=$NewPath –replace [regex]::Escape(";$RemovedFolder"),$NULL

        # Update the Environment Path
            Write-Log "Removing $RemovedFolder From Path System Variable"
            Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath

        # Show what we just did
            Write-Log "New System Path Variable is: $NewPath"
        Return $NewPath

        }