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