本帖最后由 ivor 于 2019-2-24 16:03 编辑
https://gallery.technet.microsof ... in-Recycle-6d1fbb75- #---------------------------------------------------------------------------------
- #The sample scripts are not supported under any Microsoft standard support
- #program or service. The sample scripts are provided AS IS without warranty
- #of any kind. Microsoft further disclaims all implied warranties including,
- #without limitation, any implied warranties of merchantability or of fitness for
- #a particular purpose. The entire risk arising out of the use or performance of
- #the sample scripts and documentation remains with you. In no event shall
- #Microsoft, its authors, or anyone else involved in the creation, production, or
- #delivery of the scripts be liable for any damages whatsoever (including,
- #without limitation, damages for loss of business profits, business interruption,
- #loss of business information, or other pecuniary loss) arising out of the use
- #of or inability to use the sample scripts or documentation, even if Microsoft
- #has been advised of the possibility of such damages
- #---------------------------------------------------------------------------------
-
- #requires -version 2.0
-
- Import-LocalizedData -BindingVariable Message
-
- Function Get-oscRecycleBin
- {
- <#
- .SYNOPSIS
- Get-oscRecycleBin is an advanced function which can be used to list items and delete items from recycle bin.
- .DESCRIPTION
- Get-oscRecycleBin is an advanced function which can be used to list items and delete items from recycle bin.
- .PARAMETER <ListAllItems>
- Lists all of the items of Recycle Bin.
- .PARAMETER <DeleteDaysBefore>
- Delete of specified items in Recycle Bin.
- .PARAMETER <ListDaysBefore>
- Lists of specified items in Recycle Bin.
- .EXAMPLE
- C:\PS> Get-oscRecycleBin -ListAllItems
-
- Name Type Size Deleted Days Deleted Date
- ---- ---- ---- ------------ ------------
- EnumerateInstalledPr... File folder 0.000 MB 13 Days 2/21/2012 4:44:00 PM
- edit_x64 File folder 0.000 MB 13 Days 2/21/2012 4:45:00 PM
- TestReview.zip WinRAR ZIP 0.213 MB 10 Days 2/22/2012 9:49:00 AM
- TestReview File folder 0.000 MB 10 Days 2/22/2012 10:16:00 AM
- TestReview File folder 0.000 MB 5 Days 2/22/2012 11:22:00 AM
-
- A Total of [5] items in Recycle Bin.
- .EXAMPLE
- C:\PS> Get-oscRecycleBin -DeleteDaysBefore 11
-
- Name Type Size Deleted Days Action
- ---- ---- ---- ------------ ------------
- TestReview.zip WinRAR ZIP 0.213 MB 10 Days Deleted
- TestReview File folder 0.000 MB 10 Days Deleted
- .EXAMPLE
- C:\PS> Get-oscRecycleBin -ListDaysBefore 7
-
- Name Type Size Deleted Days Deleted Date
- ---- ---- ---- ------------ ------------
- TestReview File folder 0.000 MB 5 Days 2/22/2012 11:22:00 AM
- #>
- [CmdletBinding(SupportsShouldProcess=$true)]
- Param
- (
- [Parameter(Mandatory=$true,Position=0,ParameterSetName='DeleteSpecified', `
- HelpMessage="Delete of specified items in Recycle Bin")]
- [Alias("deld")][Int32]$DeleteDaysBefore,
- [Parameter(Mandatory=$true,Position=0,ParameterSetName='ListSpecified', `
- HelpMessage="Lists of specified items in Recycle Bin")]
- [Alias("ld")][Int32]$ListDaysBefore,
- [Parameter(Mandatory=$true,Position=0,ParameterSetName='ListAll', `
- HelpMessage="Lists all of the items of Recycle Bin")]
- [Alias("all")][switch]$ListAllItems
- )
-
- BEGIN
- {
- $Objs = @()
- $Shell = New-Object -ComObject Shell.Application
- $Recycler = $Shell.NameSpace(0xa)
- }
-
- PROCESS
- {
- foreach($item in $Recycler.Items())
- {
- $Name = $item.Name
- $Type = $item.Type
- $Path = $item.Path
- $Size = "{0:N3}" -f $($item.Size/1MB)+" MB"
- $Deleted_Date = [DateTime]($Recycler.GetDetailsOf($item,2) -replace "\u200f|\u200e","")
- $Deleted_Days = "{0:N0}" -f [System.Math]::Abs((New-TimeSpan -Start `
- $(Get-Date) -End $Deleted_Date).Days) +" Days"
-
- $object = New-Object -TypeName PSObject
- $object | Add-Member -MemberType NoteProperty -Name "Name" -Value $Name
- $object | Add-Member -MemberType NoteProperty -Name "Type" -Value $Type
- $object | Add-Member -MemberType NoteProperty -Name "Size" -Value $Size
- $object | Add-Member -MemberType NoteProperty -Name "Path" -Value $Path
- $object | Add-Member -MemberType NoteProperty -Name "Deleted Days" -Value $Deleted_Days
- $object | Add-Member -MemberType NoteProperty -Name "Deleted Date" -Value $Deleted_Date
-
- $Objs += $object
- }
-
- #list all of the items in recycle bin.
- if($ListAllItems)
- {
- $PSCmdlet.WriteVerbose($Message.ListAllMsg)
- if($PSCmdlet.ShouldProcess("Recycle Bin","List all of items"))
- {
- $Objs|Select-Object "Name","Type","Size","Deleted Days","Deleted Date"| `
- Sort-Object "Deleted Date"|Format-Table
- Write-Host "A Total of [$($Objs.Count)] items in Recycle Bin.`r`n"
- }
- }
- #List of specified items in the recycle bin.
- if ($ListDaysBefore -gt 0)
- {
- $PSCmdlet.WriteVerbose($Message.ListMsg)
- if($PSCmdlet.ShouldProcess("Recycle Bin","List of specified items"))
- {
- $RecyclerItems = $Objs | Where-Object{$_."Deleted Date" -ge `
- (Get-Date).AddDays(-$ListDaysBefore)}
- if($RecyclerItems.Count -eq $null)
- {
- Write-Warning -Message "The item not found."
- }
- else
- {
- $RecyclerItems|Select-Object "Name","Type","Size","Deleted Days","Deleted Date"| `
- Sort-Object "Deleted Date"|Format-Table
- Write-Host "A Total of [$($RecyclerItems.Count)] items were deleted before $ListDaysBefore days.`r`n"
- }
- }
- }
- # Delete items that have been in the Recycle Bin more than 10 days.
- if($DeleteDaysBefore -gt 0)
- {
- $PSCmdlet.WriteVerbose($Message.DeleteMsg)
- if($PSCmdlet.ShouldProcess("Recycle Bin","Delete of specified items"))
- {
- $RecyclerItems = $Objs | Where-Object{$_."Deleted Date" -ge `
- (Get-Date).AddDays(-$DeleteDaysBefore)}
- if($RecyclerItems.Count -eq $null)
- {
- Write-Warning -Message "The item not found."
- }
- else
- {
- $RecyclerItems|Select-Object "Name","Type","Size","Deleted Days",`
- @{Name="Action";Expression={Remove-Item -Path $_.Path -Confirm:$false -Force -Recurse;
- if(Test-Path -Path $_.Path)
- {"Not Deleted"}
- else
- {"Deleted"}
- }}|Format-Table
- }
- }
- }
- }
- }
-
- Get-oscRecycleBin -DeleteDaysBefore 3
复制代码
|