functions/explorer/Invoke-PSUExplorer.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function Invoke-PSUExplorer
{
<#
    .SYNOPSIS
        Opens the windows explorer at the specified position.
     
    .DESCRIPTION
        Opens the windows explorer at the specified position.
     
    .PARAMETER Path
        Alias: FullName
        Default: (Get-Location).Path
        The folder to open in explorer. If a file was passed the containing folder will be opened instead.
     
    .PARAMETER Module
        The module, the base folder of which should be opened.
     
    .PARAMETER Duplicates
        Setting this switch will cause the function to open the same folder multiple times, if it was passed multiple times.
        By default, the function will not open the same folder multiple times (a dir of a directory with multiple files would otherwise cause multiple open windows).
     
    .PARAMETER EnableException
        This parameters disables user-friendly warnings and enables the throwing of exceptions.
        This is less user friendly, but allows catching exceptions in calling scripts.
     
    .EXAMPLE
        PS C:\> dir | Explorer
         
        Opens each folder in the current directory in a separate explorer Window.
#>

    [CmdletBinding()]
    Param (
        [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('FullName')]
        [string[]]
        $Path = (Get-Location).ProviderPath,
        
        [System.Management.Automation.PSModuleInfo[]]
        $Module,
        
        [switch]
        $Duplicates,
        
        [switch]
        $EnableException
    )
    
    Begin {
        # Contains previously opened folders
        $List = @()
    }
    Process {
        foreach ($item in $Path)
        {
            try { $resolvedPaths = Resolve-PSFPath -Path $item -Provider FileSystem }
            catch { Stop-PSFFunction -Message "Path not found: $item" -EnableException $EnableException -ErrorRecord $_ -Continue -Tag input -Target $item }
            
            foreach ($resolvedPath in $resolvedPaths)
            {
                
                $object = Get-Item $resolvedPath
                
                if ($object.PSIsContainer) { $finalPath = $object.FullName }
                else { $finalPath = $object.Directory.FullName }
                
                # If it was already opened once, skip it unless duplicates are enabled
                if ((-not $Duplicates) -and ($List -contains $finalPath))
                {
                    Write-PSFMessage -Level Verbose -Message "Skipping folder since it already was opened once: $finalPath" -Target $item -Tag skip
                    continue
                }
                
                Write-PSFMessage -Level Verbose -Message "Opening explorer at: $finalPath"
                explorer.exe $finalPath
                $List += $finalPath
            }
        }
        
        foreach ($item in $Module)
        {
            if ((-not $Duplicates) -and ($List -contains $item.ModuleBase))
            {
                Write-PSFMessage -Level Verbose -Message "Skipping folder since it already was opened once: $($item.ModuleBase)" -Target $item -Tag skip
                continue
            }
            Write-PSFMessage -Level Verbose -Message "Opening explorer at: $($item.ModuleBase)"
            explorer.exe $item.ModuleBase
            $List += $item.ModuleBase
        }
    }
}
Import-PSUAlias -Name "explorer" -Command "Invoke-PSUExplorer"