Senin, 28 November 2016

PowerShell v2 Function Switch ToParentFolder

A while back Jeff Hicks put some functions up to help navigate the file system a little more easily.  Somehow I pulled this (and one other) function-along with some aliases-to get around the system more quickly.  The aliases are w.stcf, down, and, d.  I use down the most.   The function lives in a Windows module, hence the Windows. in front of everything.
# Define function
function Windows.Switch-ToChildFolder {
 param(
  $Path = $PWD
 )

 pushd $pwd
 $dir = gci $path | Where {$_.PSIsContainer} | select fullname;
 if($dir.length -gt 0) {
  Write-Output "There are $($dir.length) children folders.";
  for($i = 1; $i -le $dir.length; $i++) {
  Write-Output "$($i): $($dir[($i-1)].fullname)";
 }
 $response = Read-Host "Type the number for the folder you want to select";
 cd $dir[$($response-1)].fullname | Out-Null;
 } else {
  Write-Output "The current directory has no children";
 }

} # end function Windows.Switch-ToChildFolder

# Set alias
Set-Alias -Name w.stcf -Value "Windows.Switch-ToChildFolder"
Set-Alias -Name Down -Value "Windows.Switch-ToChildFolder"
Set-Alias -Name d -Value "Windows.Switch-ToChildFolder"
The really cool thing here is that when you run the cmdlet it gives you a numbered list of folders into which you can go simply by typing the folder number.  For example, using down to navigate the $PSHOME directory gives me this:
PS C:> down $pshome
There are 4 children folders.
1: C:WindowsSystem32WindowsPowerShellv1.0en-US
2: C:WindowsSystem32WindowsPowerShellv1.0Examples
3: C:WindowsSystem32WindowsPowerShellv1.0Modules
4: C:WindowsSystem32WindowsPowerShellv1.0 estmodules
Type the number for the folder you want to select: 4
C:WindowsSystem32WindowsPowerShellv1.0 estmodules
PS C:WindowsSystem32WindowsPowerShellv1.0 estmodules>

lamsim

About lamsim

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :