#author("2018-09-09T15:40:38+09:00","","")
#navi(../)
* PowerShellでファイルやフォルダをごみ箱に移動する方法 [#fb08b4e5]
ファイルまたはフォルダを指定してごみ箱に移動するサンプルスクリプトを公開します。~
エラー処理等が不足していますが、参考にしてください。~
以下に使用例などを記します。~

&color(red){''使用上の注意&br;USBメモリなど外部にあるファイルはごみ箱に移動できません。&br;これはエクスプローラー操作による動作と同様です。&br;''};&br;

当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。~
あらかじめご了承ください。自己責任のもとで本資料をご利用ください。

#htmlinsertpcsp(win-top.html,win-sp.html)

* 参考資料 [#kfd0460d]
-[[powershellでゴミ箱に移動>https://social.technet.microsoft.com/Forums/office/ja-JP/505a55ac-9ba5-40c8-a551-a6251fa9b376/powershell?forum=Sharepoint2013]]


* 本資料で使用したWindowsおよびPowerShellのバージョン [#s2c2ef3f]
 PS C:\> [System.Environment]::OSVersion | Format-List
 
 Platform      : Win32NT
 ServicePack   :
 Version       : 10.0.17134.0
 VersionString : Microsoft Windows NT 10.0.17134.0
 
 PS C:\> $PSVersionTable
 
 Name                           Value
 ----                           -----
 PSVersion                      5.1.17134.228
 PSEdition                      Desktop
 PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
 BuildVersion                   10.0.17134.228
 CLRVersion                     4.0.30319.42000
 WSManStackVersion              3.0
 PSRemotingProtocolVersion      2.3
 SerializationVersion           1.1.0.1

* サンプルスクリプト [#v90520c4]
以下のサンプルスクリプトは2つのファンクションを記述してあります。~
+ Folder-ToRecycleBin ファイル名~
引数に指定したフォルダをごみ箱に移動します。
+ File-ToRecycleBin フォルダ名~
引数に指定したファイルをごみ箱に移動します。

&ref(ToRecycle.zip); : (SJIS, CRLF)

 Add-Type -AssemblyName Microsoft.VisualBasic
 
 # フォルダをゴミ箱に移動する
 function Folder-ToRecycleBin($target_dir_path) {
   if ((Test-Path $target_dir_path) -And ((Test-Path -PathType Container (Get-Item $target_dir_path)))) {
     $fullpath = (Get-Item $target_dir_path).FullName
     [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
   } else {
     Write-Output "'$target_dir_path' is not directory or not found."
   }
 }
 
 # ファイルをゴミ箱に移動する
 function File-ToRecycleBin($target_file_path) {
   if ((Test-Path $target_file_path) -And ((Test-Path -PathType Leaf (Get-Item $target_file_path)))) {
     $fullpath = (Get-Item $target_file_path).FullName
     [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
   } else {
     Write-Output "'$target_file_path' is not file or not found."
   }
 }
 

* 使用方法 [#k5b2aae1]
以下、実際に上記スクリプトを使用してみます。
+ 上記スクリプトコピーし、PowerShellコンソールに貼り付けました。~
(PowerShellの[[実行セキュリティポリシー>PowerShell/PowerShellスクリプトを起動できるようにする]]を変更していなかったので、Pasteしました。)
#ref(00.png)
#br
+ごみ箱移動対象とするファイルとフォルダを作成してみます。
 PS C:\work> (Get-Location).Path
 C:\work
 PS C:\work> Get-ChildItem *
 PS C:\work> "Hello" | Out-File hello.txt -Encoding default
 PS C:\work> New-Item folder -Type Directory
 
     ディレクトリ: C:\work
 
 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 d-----       2018/09/09     15:17                folder
 
 PS C:\work> Get-ChildItem
 
     ディレクトリ: C:\work
 
 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 d-----       2018/09/09     15:17                folder
 -a----       2018/09/09     15:17              7 hello.txt
#ref(01.png)
#br
#ref(02.png)
#br
+ファイルをごみ箱に移動してみます。
++ファイル移動までのごみ箱の状態
#ref(03.png)
#br
++以下のコマンドを実行
 File-ToRecycleBin .\hello.txt
#ref(04.png)
#br
++ごみ箱を確認すると、指定したファイルが移動されています。
#ref(05.png)
#br
++フォルダのを確認すると、指定したファイルが消えています。
#ref(06.png)
#br
+フォルダ(ディレクトリ)をごみ箱に移動してみます。
++以下のコマンドを実行
 Folder-ToRecycleBin .\folder\
#ref(07.png)
#br
++ごみ箱を確認すると、指定したフォルダが移動されています。
#ref(08.png)
#br
++フォルダのを確認すると、指定したフォルダが消えています。
#ref(09.png)
#br

以上のように、ファイルおよびフォルダがごみ箱に移動されています。~
&color(red){''再度注意!''&br;USBメモリなどの外部ストレージの場合、移動されずに削除されます。&br;これは、エクスプローラーでの操作と同じになりますのご注意ください。};

* Aliasを使って便利に使う [#na0aa22d]
上記の2つのファンクションをAliasとして設定すれば、便利に使えるかもしれません。~
Set-Aliasで本ファンクションを設定しています。
 Remove-Item Alias:del
 Set-Alias -name del -value File-ToRecycleBin

 Remove-Item Alias:rmdir
 Set-Alias -name rmdir -value Folder-ToRecycleBin
 
 上記のエイリアス設定により、del, rmdirでファイルを指定すると、ごみ箱に移動されます。


以上、PowerShellでファイルおよびフォルダをゴミ箱に移動するサンプルスクリプトでした。

#htmlinsertpcsp(win-btm.html,win-sp.html)


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS