Skip to content

Instantly share code, notes, and snippets.

@t2psyto
Created March 27, 2024 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t2psyto/0e458df45a11805cc8908d671c76474d to your computer and use it in GitHub Desktop.
Save t2psyto/0e458df45a11805cc8908d671c76474d to your computer and use it in GitHub Desktop.
openfolder
function openfolder() {
# System.Windows.Formsアセンブリを有効化
[void][System.Reflection.Assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")
# OpenFileDialogクラスをインスタンス化し、必要な情報を設定
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.InitialDirectory = "C:\"
$dialog.Title = "フォルダを選択してください"
$dialog.ValidateNames = 0 # falseに設定
$dialog.CheckFileExists = 0 # falseに設定
$dialog.CheckPathExists = 1 # trueに設定
# 常に表示されるファイル名(なんか文字数制限ある感じになってしまった・・・)
$dialog.FileName = "フォルダを選択"
# ダイアログを表示
if($dialog.ShowDialog() -eq "OK"){
# [OK]ボタンがクリックされたら、選択されたファイル名(パス)を表示
# 今のままだと[パス]/フォルダを選択というパスになってしまうので親要素だけを切り出し
$folderPath = Split-Path -Parent $dialog.FileName
return $folderPath
}
}
$folderPath = openfolder
[System.Windows.Forms.MessageBox]::Show($folderPath + " が選択されました。", "できたよ")
@t2psyto
Copy link
Author

t2psyto commented Mar 27, 2024

ref:
自動化の魔法: 【PowerShell】OpenFileDialogでフォルダ選択をする
https://lilas-automation-magic.blogspot.com/2020/06/powershellopenfiledialog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment