Created
April 5, 2016 13:03
-
-
Save gravejester/358f201c6d0654590c4452aad6aef556 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-PathShortener { | |
<# | |
.SYNOPSIS | |
Path Shortener | |
.NOTES | |
Author: Øyvind Kallstad | |
Date: 05.04.2016 | |
Version: 2 | |
#> | |
[CmdletBinding()] | |
param ( | |
# Path to shorten. | |
[Parameter(Position = 0)] | |
[ValidateNotNullorEmpty()] | |
[string] $Path, | |
# Number of parts to keep before truncating. Default value is 2. | |
[Parameter()] | |
[ValidateRange(0, [int32]::MaxValue)] | |
[int] $KeepBefore = 2, | |
# Number of parts to keep after truncating. Default value is 1. | |
[Parameter()] | |
[ValidateRange(1, [int32]::MaxValue)] | |
[int] $KeepAfter = 1, | |
# Path separator character. | |
[Parameter()] | |
[string] $Separator = [System.IO.Path]::DirectorySeparatorChar, | |
# Truncate character(s). Default is '...' | |
# Use '[char]8230' to use the horizontal ellipsis character instead. | |
[Parameter()] | |
[string] $TruncateChar = '...' | |
) | |
$splitPath = $Path.Split($Separator, [System.StringSplitOptions]::RemoveEmptyEntries) | |
if ($splitPath.Count -gt ($KeepBefore + $KeepAfter)) { | |
$outPath = [string]::Empty | |
for ($i = 0; $i -lt $KeepBefore; $i++) { | |
$outPath += $splitPath[$i] + $Separator | |
} | |
$outPath += "$($TruncateChar)$($Separator)" | |
for ($i = ($splitPath.Count - $KeepAfter); $i -lt $splitPath.Count; $i++) { | |
if ($i -eq ($splitPath.Count - 1)) { | |
$outPath += $splitPath[$i] | |
} | |
else { | |
$outPath += $splitPath[$i] + $Separator | |
} | |
} | |
} | |
else { | |
$outPath = $splitPath -join $Separator | |
if ($splitPath.Count -eq 1) { | |
$outPath += $Separator | |
} | |
} | |
Write-Output $outPath | |
} | |
$pathStrings = @( | |
'C:\', | |
'c:', | |
'C:\Users\ojk\Documents\Visual Studio 2015\Projects', | |
'c:\windows', | |
'c:\windows\', | |
'C:\Dell\Drivers', | |
'C:\Dell\Drivers\2K0JJ', | |
'\\server01\rootshare\subfolder1', | |
'\\server01\rootshare\subfolder1\subfolder2', | |
'\\server01\rootshare\subfolder1\subfolder2\subfolder3', | |
'HKLM:', | |
'HKLM:\SOFTWARE', | |
'HKLM:\SOFTWARE\Microsoft', | |
'HKLM:\SOFTWARE\Microsoft\Windows NT', | |
'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion', | |
'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console', | |
'\\server02\c$\users\user01\subfolder1' | |
) | |
foreach ($pathString in $pathStrings) { | |
#$pathString | |
Invoke-PathShortener -Path $pathString -Verbose -KeepBefore 2 -KeepAfter 2 -TruncateChar '...' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment