functions/explorer/Set-PSUDrive.ps1

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
33
34
35
36
37
38
39
40
41
42
43
44
45
function Set-PSUDrive
{
<#
    .SYNOPSIS
        Creates a new psdrive, and moves location to it.
     
    .DESCRIPTION
        Will create a PSDrive, by default in the current path.
        This allows swiftly reducing path length.
        Then it will immediately change location to the new drive.
     
    .PARAMETER Name
        What to name the new PSDrive?
     
    .PARAMETER Root
        Default: .
        The root of the new drive.
     
    .EXAMPLE
        PS C:\> set-as pr
     
        Sets the current path as drive "pr" and sets it as the current location.
     
    .NOTES
        Author: Donovan Brown
        Source: http://donovanbrown.com/post/Shorten-your-PowerShell-directory-path
     
        Thank you for sharing and granting permission to use this convenience :)
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        $Name,
        
        [string]
        $Root = "."
    )
    
    $path = Resolve-Path $Root
    $null = New-PSDrive -PSProvider $path.Provider -Name $Name -Root $Root -Scope Global
    Set-Location -LiteralPath "$($Name):"
}

Import-PSUAlias -Name "set-as" -Command "Set-PSUDrive"