반응형
PowerShell을 사용하여 파일을 휴지통으로 이동하는 방법은 무엇입니까?
사용 시rm
Powershell에서 파일을 삭제하는 명령입니다. 파일은 영구적으로 삭제됩니다.
UI를 통해 파일을 삭제할 때와 같이 삭제된 항목을 휴지통에 넣었으면 합니다.
PowerShell에서 이 작업을 수행하는 방법은 무엇입니까?
2017년 답변: 재활용 모듈 사용
Install-Module -Name Recycle
그런 다음 실행:
Remove-ItemSafely file
라는 가명을 쓰는 것을 좋아합니다.trash
이를 위하여
확인 프롬프트를 항상 표시하지 않으려면 다음을 사용합니다.
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('d:\foo.txt','OnlyErrorDialogs','SendToRecycleBin')
(솔루션 제공: Shay Levy)
Chris Balance의 JScript 솔루션과 거의 동일한 방식으로 PowerShell에서 작동합니다.
$shell = new-object -comobject "Shell.Application"
$folder = $shell.Namespace("<path to file>")
$item = $folder.ParseName("<name of file>")
$item.InvokeVerb("delete")
다음은 작업량을 줄이는 짧은 버전입니다.
$path = "<path to file>"
$shell = new-object -comobject "Shell.Application"
$item = $shell.Namespace(0).ParseName("$path")
$item.InvokeVerb("delete")
다음은 입력뿐만 아니라 디렉터리도 지원하는 향상된 기능입니다.
Add-Type -AssemblyName Microsoft.VisualBasic
function Remove-Item-ToRecycleBin($Path) {
$item = Get-Item -Path $Path -ErrorAction SilentlyContinue
if ($item -eq $null)
{
Write-Error("'{0}' not found" -f $Path)
}
else
{
$fullpath=$item.FullName
Write-Verbose ("Moving '{0}' to the Recycle Bin" -f $fullpath)
if (Test-Path -Path $fullpath -PathType Container)
{
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
}
else
{
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
}
}
}
RecycleBin으로 파일 제거:
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('e:\test\test.txt','OnlyErrorDialogs','SendToRecycleBin')
RecycleBin으로 폴더 제거:
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::Deletedirectory('e:\test\testfolder','OnlyErrorDialogs','SendToRecycleBin')
여기 sba923s의 훌륭한 답변에 대한 약간의 모드가 있습니다.
매개 변수 전달과 같은 몇 가지를 변경하고 추가했습니다.-WhatIf
파일 또는 디렉토리에 대한 삭제를 테스트합니다.
function Remove-ItemToRecycleBin {
Param
(
[Parameter(Mandatory = $true, HelpMessage = 'Directory path of file path for deletion.')]
[String]$LiteralPath,
[Parameter(Mandatory = $false, HelpMessage = 'Switch for allowing the user to test the deletion first.')]
[Switch]$WhatIf
)
Add-Type -AssemblyName Microsoft.VisualBasic
$item = Get-Item -LiteralPath $LiteralPath -ErrorAction SilentlyContinue
if ($item -eq $null) {
Write-Error("'{0}' not found" -f $LiteralPath)
}
else {
$fullpath = $item.FullName
if (Test-Path -LiteralPath $fullpath -PathType Container) {
if (!$WhatIf) {
Write-Verbose ("Moving '{0}' folder to the Recycle Bin" -f $fullpath)
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
}
else {
Write-Host "Testing deletion of folder: $fullpath"
}
}
else {
if (!$WhatIf) {
Write-Verbose ("Moving '{0}' file to the Recycle Bin" -f $fullpath)
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
}
else {
Write-Host "Testing deletion of file: $fullpath"
}
}
}
}
$tempFile = [Environment]::GetFolderPath("Desktop") + "\deletion test.txt"
"stuff" | Out-File -FilePath $tempFile
$fileToDelete = $tempFile
Start-Sleep -Seconds 2 # Just here for you to see the file getting created before deletion.
# Tests the deletion of the folder or directory.
Remove-ItemToRecycleBin -WhatIf -LiteralPath $fileToDelete
# PS> Testing deletion of file: C:\Users\username\Desktop\deletion test.txt
# Actually deletes the file or directory.
# Remove-ItemToRecycleBin -LiteralPath $fileToDelete
다음은 사용자 프로필에 추가하여 'rm'이(가) 휴지통에 파일을 보내도록 할 수 있는 완벽한 솔루션입니다.제한된 테스트에서는 이전 솔루션보다 상대 경로를 더 잘 처리합니다.
Add-Type -AssemblyName Microsoft.VisualBasic
function Remove-Item-toRecycle($item) {
Get-Item -Path $item | %{ $fullpath = $_.FullName}
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($fullpath,'OnlyErrorDialogs','SendToRecycleBin')
}
Set-Alias rm Remove-Item-toRecycle -Option AllScope
언급URL : https://stackoverflow.com/questions/502002/how-do-i-move-a-file-to-the-recycle-bin-using-powershell
반응형
'programing' 카테고리의 다른 글
Spring 데이터가 포함된 날짜별 ASC 주문 (0) | 2023.08.07 |
---|---|
Express 명령을 찾을 수 없습니다. (0) | 2023.08.07 |
보류 중인 "누락"을 해결하는 방법안드로이드 api 30+의 의도 변이성 플래그" 린트 경고? (0) | 2023.08.07 |
MYSQL, 선택한 필드를 한 테이블에서 다른 테이블로 복사 (0) | 2023.08.07 |
PHP - 배열을 클래스 상수로 사용합니다. (0) | 2023.08.07 |