-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStart-ExplorerHere.ps1
More file actions
88 lines (68 loc) · 2.74 KB
/
Start-ExplorerHere.ps1
File metadata and controls
88 lines (68 loc) · 2.74 KB
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
<#
.SYNOPSIS
Starts Windows Explorer here or in specified path
.DESCRIPTION
Starts an instance of Windows Explorer (the file one, not the internet one) in the current folder
or in a specified location. Can be used with alternate credentials (a la 'run as').
.PARAMETER Path
By default, the current location. Can be any valid path on the system (and some invalid ones)
.PARAMETER RunAs
This will prompt for username and password to 'Run As'
.EXAMPLE
Start-ExplorerHere.ps1
Starts explorer in the current folder
.EXAMPLE
Start-ExplorerHere.ps1 -path C:\Windows
Starts explorer in the C:\Windows folder
.EXAMPLE
Start-ExplorerHere.ps1 -path C:\Windows\readme.txt
Starts explorer in the C:\Windows folder (it ignores the file portion)
.EXAMPLE
Start-ExplorerHere.ps1 -RunAs
Starts explorer in the current folder, prompting for username and password
.EXAMPLE
Start-ExplorerHere.ps1 -Admin
Starts explorer in the current folder as admin (with UAC prompt if enabled)
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$false,ValueFromPipeline=$true,HelpMessage="Enter a valid directory path")]
[Alias('Folder', 'Directory', 'Dir', 'Location')]
[string[]] $Path = $PWD,
[Parameter(Mandatory=$false,ValueFromPipeline=$false,HelpMessage="Enter a valid username")]
[Alias('As', 'User', 'Alternate')]
[switch] $RunAs = $false,
[Parameter(Mandatory=$false,ValueFromPipeline=$false,HelpMessage="Enter a valid username")]
[Alias('Admin')]
[switch] $AsAdmin = $false
)
BEGIN { }
PROCESS {
foreach ($dir in $Path) {
[bool] $DoIt = $true
Switch ($dir) {
# Is a normal directory
{ test-path $_ -PathType Container } { $dir = (Resolve-Path $_).ProviderPath; break }
# Is a file, add the parent folder
{ test-path $_ -PathType leaf } { $dir = (Resolve-Path (Split-Path $_ -Parent)).ProviderPath; break }
#Isn't valid - don't process...
default { Write-Host "Path `"$dir`" is not a valid directory"; $DoIt = $false; break}
}
if ($DoIt) {
write-verbose "Starting Explorer in path `"$dir`""
if ($RunAs) {
#Note: /separate might be needed, or might not work
#(seems dead maybe around vista)
#BUT, try "/separate","/root,$dir" as the ArgumentList if necessary
start-process explorer.exe -ArgumentList $dir -verb runasuser
}
elseif ($AsAdmin) {
start-process explorer.exe -ArgumentList $dir -Verb runas
}
else {
start-process explorer.exe -ArgumentList $dir
}
}
}
}
END { }