forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress-EnvironmentVariables.ps1
More file actions
32 lines (29 loc) · 953 Bytes
/
Copy pathCompress-EnvironmentVariables.ps1
File metadata and controls
32 lines (29 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<#
.SYNOPSIS
Replaces each of the longest matching parts of a string with an embedded environment variable with that value.
.EXAMPLE
Compress-EnvironmentVariables.ps1 'C:\Program Files\Git\bin\git.exe'
%ProgramFiles%\Git\bin\git.exe
#>
#Requires -Version 3
[CmdletBinding()] Param(
# The string to generalize with environment variable substitution.
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$true)][string] $Value
)
Begin
{
$envs = Get-ChildItem env: |where Name -notin 'ProgramW6432','ALLUSERSPROFILE','PROCESSOR_LEVEL','NUMBER_OF_PROCESSORS'
function Get-EnvMatch([string]$value)
{
return $envs |
where {$Value.IndexOf($_.Value,[StringComparison]::CurrentCultureIgnoreCase) -gt -1} |
sort {$_.Value.Length} -Descending |
select -First 1
}
}
Process
{
for($env = Get-EnvMatch $Value; $env; $env = Get-EnvMatch $Value)
{$Value = $Value -replace "$([regex]::Escape($env.Value))","%$($env.Name)%"}
return $Value
}