Created
          March 27, 2024 15:19 
        
      - 
      
- 
        Save t2psyto/0e458df45a11805cc8908d671c76474d to your computer and use it in GitHub Desktop. 
    openfolder 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 + " が選択されました。", "できたよ") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
ref:
自動化の魔法: 【PowerShell】OpenFileDialogでフォルダ選択をする
https://lilas-automation-magic.blogspot.com/2020/06/powershellopenfiledialog.html