-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-FolderName.ps1
66 lines (56 loc) · 2.38 KB
/
Get-FolderName.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
function Get-FolderName {
<#
.SYNOPSIS
Gets a foldername through the native OpenFileDialog.
.DESCRIPTION
Gets a foldername through the native OpenFileDialog. If user clicks 'OK' an [array] is returned, otherwise returns
a $null if the dialog is canceled. Aliased function to 'Get-Folder' for backward compatibility
.PARAMETER Path
String indicating the initial path selected in the dialog. Aliased to 'InitialDirectory', 'RootFolder'
.PARAMETER InitialDirectory
The directory for the OpenFileDialog to start in. Defaults to $pwd.
Aliased to 'Path'.
.PARAMETER NoNewFolder
Switch which controls whether 'New folder' button appears in the dialog box.
.PARAMETER Title
String indicating the Title of the dialog box. Defaults to 'Select a folder'
.EXAMPLE
PS C:\> $Folder = Get-FolderName
Will present a folderopen dialog box and save the selection to '$Folder'
.NOTES
Inspiration: Part of the ISEColorThemeCmdlets.ps1 Script by Jeff Pollock
http://gallery.technet.microsoft.com/ISE-Color-Theme-Cmdlets-24905f9e
# Source: https://gallery.technet.microsoft.com/ISE-Color-Theme-Cmdlets-24905f9e
# get-help about_ISE-Color-Theme-Cmdlets for more information
#>
[CmdletBinding(ConfirmImpact = 'None')]
[alias('Get-Folder')] #FunctionAlias
[OutputType([string[]])]
Param(
[Alias('InitialDirectory', 'RootFolder')]
[string] $Path = $pwd,
[switch] $NoNewFolder,
[Alias('Description')]
[string] $Title = 'Select a folder'
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowserDialog = New-Object -TypeName System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.RootFolder = 'MyComputer'
$FolderBrowserDialog.SelectedPath = $Path
if ($NoNewFolder) { $FolderBrowserDialog.ShowNewFolderButton = $false }
if ($Title) { $FolderBrowserDialog.Description = $Title }
$Result = $FolderBrowserDialog.ShowDialog()
# needed to play around to force PowerShell to return an array.
if ($Result -eq 'OK') {
[array] $ReturnArray = $FolderBrowserDialog.SelectedPath
Write-Output -InputObject (, $ReturnArray)
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}