![](https://powershell.work/wp-content/themes/cocoon-master/images/b-woman.png)
PowerShellで
・フルパスから
・拡張子を取得
できます!
コード
ここでは例として
・デスクトップ配下のファイル「aiueo.txt」の
・拡張子を取得
します。
![ファイル「aiueo.txt」](https://powershell.work/wp-content/uploads/2022/11/ps_get_file_extension_001.png)
# 対象ファイル
$targetFile = "C:\Users\user\Desktop\aiueo.txt"
$ws = New-Object -ComObject Wscript.Shell
# 対象ファイルの存在有無を確認
if (-Not(Test-Path $targetFile)) {
$ws.popup("対象ファイルが存在しないため、処理を終了します。")
exit
}
# 拡張子を取得
$folderPath = (Get-ChildItem $targetFile).Extension
$ws.popup("拡張子:" + $folderPath)
実行結果
フルパスから拡張子を取得できました。
※「.txt」を取得できました。
![実行結果](https://powershell.work/wp-content/uploads/2022/11/ps_get_file_extension_002.png)
参考
上記で使用した以下の詳細は、公式サイトをご確認ください。
●「Test-Path」コマンドレット
●「Get-ChildItem」コマンドレット