4
9

More than 5 years have passed since last update.

PowerShellメモ フォルダ選択ダイアログ

Last updated at Posted at 2016-10-08

概要

フォルダ選択ダイアログでフォルダパスを取得する関数。

コード

Add-Type -AssemblyName System.Windows.Forms

<#
.SYNOPSIS
    フォルダ選択ダイアログ表示

.DESCRIPTION
    フォルダ選択ダイアログを表示し、選択したフォルダパスを返す。

.PARAMETER Description
    ダイアログに表示する説明文(省略可)

.PARAMETER CurrentDefault
    カレントディレクトリをダイアログの初期フォルダとするか否か(省略可)

.OUTPUTS
    選択したフォルダパス。キャンセル時はnull
#>
function Get-FolderPathG
{
    param(
        [Parameter(ValueFromPipeline=$true)]
        [string]$Description = "フォルダを選択してください",
        [boolean]$CurrentDefault = $false
    )
    # メインウィンドウ取得
    $process = [Diagnostics.Process]::GetCurrentProcess()
    $window = New-Object Windows.Forms.NativeWindow
    $window.AssignHandle($process.MainWindowHandle)

    $fd = New-Object System.Windows.Forms.FolderBrowserDialog
    $fd.Description = $Description

    if($CurrentDefault -eq $true){
        # カレントディレクトリを初期フォルダとする
        $fd.SelectedPath = (Get-Item $PWD).FullName
    }

    # フォルダ選択ダイアログ表示
    $ret = $fd.ShowDialog($window)

    if($ret -eq [System.Windows.Forms.DialogResult]::OK){
        return $fd.SelectedPath
    }
    else{
        return $null
    }
}

実行例

p03.PNG

動作確認した環境

  • PowerShell V4 (Windows 8.1)
  • PowerShell V5 (Windows 10)
4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9