Skip to content

Instantly share code, notes, and snippets.

@kinuasa
Last active July 19, 2025 15:06
Show Gist options
  • Save kinuasa/8c129c3def2789dbb9608fb4491565ae to your computer and use it in GitHub Desktop.
Save kinuasa/8c129c3def2789dbb9608fb4491565ae to your computer and use it in GitHub Desktop.
ドラッグ&ドロップでファイルのパスを取得するPowerShellスクリプト 関連記事:https://note.com/kinuasa/n/nbd74a7da8458
Add-Type -AssemblyName "PresentationFramework"
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="FilePicker" Height="250" Width="350" AllowDrop="True" Topmost="True">
<Grid>
<ListBox Name="FileListBox" AllowDrop="True"/>
<TextBlock Name="HintText"
Text="ここにファイルをドラッグ&ドロップしてください"
Foreground="Gray"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsHitTestVisible="False" />
</Grid>
</Window>
"@
$reader = [System.Xml.XmlReader]::Create([System.IO.StringReader]$xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$fileListBox = $window.FindName("FileListBox")
$files = @()
$fileListBox.Add_DragOver({
if ($_.Data.GetDataPresent([Windows.DataFormats]::FileDrop)) {
$_.Effects = [Windows.DragDropEffects]::Copy
} else {
$_.Effects = [Windows.DragDropEffects]::None
}
$_.Handled = $true
})
$fileListBox.Add_Drop({
if ($_.Data.GetDataPresent([Windows.DataFormats]::FileDrop)) {
$script:files = $_.Data.GetData([Windows.DataFormats]::FileDrop)
#foreach ($file in $script:files) {$fileListBox.Items.Add($file)} #確認用
$window.Close()
}
})
$window.ShowDialog() | Out-Null
Write-Host "ドロップされたファイル:"
$files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment