functions/explorer/Invoke-PSUDesktop.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
46
47
48
49
function Invoke-PSUDesktop
{
    <#
        .SYNOPSIS
            Function that sets the current console path to the user desktop.
         
        .DESCRIPTION
            Function that sets the current console path to the user desktop.
            Uses the current user's desktop by default, but can be set to the desktop of any locally available profile.
         
        .PARAMETER User
            Alias: u
            Choose which user's desktop path to move to. Must be available as a local profile for things to work out.
     
        .PARAMETER Get
            Alias: g
            Returns the path, rather than changing the location
         
        .EXAMPLE
            PS C:\> Desktop
     
            Sets the current location to the desktop path of the current user.
    #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseOutputTypeCorrectly", "")]
    [CmdletBinding()]
    Param (
        [Parameter(Position = 0)]
        [Alias('u')]
        [string]
        $User = $env:USERNAME,
        
        [Alias('g')]
        [switch]
        $Get
    )
    
    # Default Path for current user
    $Path = "$env:SystemDrive\Users\$User\Desktop"
    
    if (-not (Test-Path $Path))
    {
        Stop-PSFFunction -Message "Path to Desktop not found: $Path" -Tag fail -Target $User -Category InvalidArgument
        return
    }
    
    if ($Get) { return $Path }
    else { Push-Location $Path }
}
Import-PSUAlias -Name "desktop" -Command "Invoke-PSUDesktop"