![](https://powershell.work/wp-content/themes/cocoon-master/images/b-woman.png)
PowerShellで
・ファイルを移動
できます!
コード
ここでは例として
・デスクトップ配下のファイル「aiueo.txt」を
・フォルダ「folder_001」へ移動
します。
![ファイル「aiueo.txt」](https://powershell.work/wp-content/uploads/2022/07/ps_move_file_001.png)
# 移動するファイルのパス
$targetFilePath = "C:\Users\user\Desktop\aiueo.txt"
# 移動後のフォルダのパス
$newFolderPath="C:\Users\user\Desktop\folder_001"
$ws = New-Object -ComObject Wscript.Shell
try
{
# ファイル名を移動
Move-Item -Force $targetFilePath $newFolderPath -ErrorAction Stop
$ws.popup("ファイルを移動しました。")
}
catch
{
$ws.popup("エラー : " + $PSItem)
}
実行結果
ファイルを移動できました。
![実行結果①](https://powershell.work/wp-content/uploads/2022/07/ps_move_file_002.png)
![実行結果②](https://powershell.work/wp-content/uploads/2022/07/ps_move_file_003.png)
参考①
フォルダの移動もできます。
詳細は以下の記事をご確認ください。
XXX
参考②
上記のコードで使用した以下の詳細は、公式サイトをご確認ください。
●「Move-Item」コマンドレット
●例外処理について