Tag Archives: Shell.Application

Get-RecycleBin – Shows the contents of the Recycle Bin

As I posted earlier in the Clear-RecycleBin New PowerShell 5.0 cmdlet, available in Windows 10 article, in Windows 10 it is now possible to clear the Recycle Bin. Because the functionality to actually view which files is not implemented in any cmdlet at the moment I decided to upload a new function, Get-RecycleBin, to the TechNet Script Gallery.

Get-RecycleBin

Using the this function it is possible to view the contents of the recycle bin, including the filename, size and the current path in the Recycle Bin folder.

.EXAMPLE
Get-RecycleBin

Description
-----------
Executing this function will display the name, size and path of the files stored in the Recycle Bin for the current user

For more information or the direct download links of these scripts please refer to the links below. Feel free to leave a comment either here or in the TechNet Script Library.

TechNet Script Gallery
My entries in TechNet Script Gallery
JaapBrasser.com – Scripts
Get-RecycleBin – shows the contents of the Recycle Bin

Clear-RecycleBin New PowerShell 5.0 cmdlet, available in Windows 10

In PowerShell 5.0 a new cmdlet has been introduced that provides the ability to clear the Recycle Bin programmatically. It supports the following four parameters:

  1. DriveLetter
  2. Force
  3. WhatIf
  4. Confirm

Unfortunately there is no Get-RecycleBin cmdlet, for this purpose I have written a short function that enumerates the contents of Recycle Bin for the current user:

1
2
3
4
Function Get-RecycleBin {
    (New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |
	Select-Object Name,Size,Path
}

Get-RecycleBin

By verifying this output we can now determine if we would like to Clear the Recycle Bin or if some files that are stored there should be recovered before deleting all files. The following command will delete all files from the Recycle Bin for all drive letters:

1
Clear-RecycleBin

Clear-RecycleBin

The default mode of operation of this cmdlet is to ask for confirmation before deleting all files. This is similar behavior to right clicking the icon of Recycle Bin and clicking ‘Empty Recycle Bin’. To delete the files without confirmation the following code can be used:

1
Clear-RecycleBin -Force

Alternatively the Confirm parameter can also be explicitly set to $false:

1
Clear-RecycleBin -Confirm:$false

Unfortunately there is no online help available for this cmdlet, so for more information about this cmdlet please refer to the built-in help in PowerShell:

1
Get-Help Clear-RecycleBin -ShowWindow

For more information or the direct download links of these scripts please refer to the links below. Feel free to leave a comment either here or in the TechNet Script Library.

TechNet Script Gallery
My entries in TechNet Script Gallery
Get-RecycleBin – shows the contents of the Recycle Bin

New article on PowerShell Magazine: Use Shell.Application to determine which folder is open in File Explorer

Explorer

To determine which Windows are currently open in Explorer the Shell.Application Com Object can be used. Specifically the Windows() method of this object. It can retrieve the open Windows of both explorer as well as Internet Explorer. The full article  is available on PowerShell Magazine : Use Shell.Application to determine which folder is open in File Explorer

1
2
3
$ShellApp = New-Object -ComObject Shell.Application
$ShellApp.Windows() | Where-Object {$_.Name -eq 'File Explorer'} | 
Select-Object LocationName,LocationURL

For more articles like this, have a look at the External Articles section of my blog, it contains all the articles I have posted on external sources such as PowerShell Magazine.

Links in this Article
PSTip: Use Shell.Application to determine which folder is open in File Explorer
PowerShell Magazine
External Articles

New article on PowerShell Magazine: Use Shell.Application to display extended file attributes

My latest article on PowerShell Magazine is on how to use Shell.Application Com object to display extended file attributes in PowerShell. Because this is heavily dependent on the Windows version and localization this does present some challenges. The full article containing tips on how to use this class is available on PowerShell Magazine : Use Shell.Application to display extended file attributes

The following example will list the first three attributes using the Shell.Application object:

$com = (New-Object -ComObject Shell.Application).NameSpace('C:\')
$com.Items() | ForEach-Object {
 New-Object -TypeName PSCustomObject -Property @{
 Name = $com.GetDetailsOf($_,0)
 Size = $com.GetDetailsOf($_,1)
 ItemType = $com.GetDetailsOf($_,2)
 }
}

For more articles like this, have a look at the External Articles section of my blog, it contains all the articles I have posted on external sources such as PowerShell Magazine. The article posted on PowerShell magazine contains a link to my Get-ExtensionAttribute script in the TechNet Script Gallery.

Links in this Article
PSTip: Use Shell.Application to display extended file attributes
PowerShell Magazine
External Articles
Get-ExtensionAttribute
My entries in TechNet Script Gallery

Get-ExtensionAttribute – Script in TechNet Script Gallery

Previously I blogged about listing extended file and folder attributes using the Shell.Application object in PowerShell in my List length and bitrate for mp3 files using Shell.Application article. As a result of that post I decided to write a function, Get-ExtensionAttribute, to properly and consistently be able to list extension attributes even when the locale or version of windows is different.

For this purpose I have created a dynamic parameter -ExtensionAttribute which retrieves all extension attributes available on the system it is run which can be selected and displayed. The function takes path names from either pipeline or as a single path or collection of paths.

.EXAMPLE
Get-ExtensionAttribute -FullName C:\Music -ExtensionAttribute Size,Length,Bitrate

Description
-----------
Retrieves the Size,Length,Bitrate and FullName of the contents of the C:\Music folder, non recursively

.EXAMPLE
Get-ExtensionAttribute -FullName C:\Music\Song2.mp3,C:\Music\Song.mp3 -ExtensionAttribute Size,Length,Bitrate

Description
-----------
Retrieves the Size,Length,Bitrate and FullName of Song.mp3 and Song2.mp3 in the C:\Music folder

.EXAMPLE
Get-ChildItem -Recurse C:\Video | Get-ExtensionAttribute -ExtensionAttribute Size,Length,Bitrate,Totalbitrate

Description
-----------
Uses the Get-ChildItem cmdlet to provide input to the Get-ExtensionAttribute function and retrieves selected attributes for the C:\Videos folder recursively

.EXAMPLE
Get-ChildItem -Recurse C:\Music | Select-Object FullName,Length,@{Name = 'Bitrate' ; Expression = { Get-ExtensionAttribute -FullName $_.FullName -ExtensionAttribute Bitrate | Select-Object -ExpandProperty Bitrate } }

Description
-----------
Combines the output from Get-ChildItem with the Get-ExtensionAttribute function, selecting the FullName and Length properties from Get-ChildItem with the ExtensionAttribute Bitrate

The complete function is available in the TechNet Script Library. To view this script or to participate in the discussions about this script either comment here or in the TechNet Script Library.

TechNet Script Library
My entries in TechNet Script Gallery
Get-ExtensionAttribute

List length and bitrate for mp3 files using Shell.Application

I recently got the request how to list the Bit Rate and Length for mp3 files. These properties are not available when using the Get-ChildItem cmdlet, but can be revealed when using the Shell.Application ComObject.

$com = (New-Object -ComObject Shell.Application).NameSpace('C:\Music')
# Determine correct index for Bitrate and Length, change names based on installed locale on your system
for( $index = 0; ((-not $bitrateAttribute) -or (-not $lengthattribute)); ++$index ) {
 $name = $com.GetDetailsOf($com.Items,$index)
 if ($name -eq 'Length') {$lengthattribute = $index}
 if ($name -eq 'Bit rate') {$bitrateAttribute = $index}
}
$com.Items() | ForEach-Object {
 # Create custom objects so output can be manipulated easier, also allows for structered output
 New-Object -TypeName PSCustomObject -Property @{
 Name = $_.Name
 Size = $com.GetDetailsOf($_, 1)
 DateCreated = $com.GetDetailsOf($_, 4)
 Length = $com.GetDetailsOf($_,$lengthattribute)
 BitRate = $com.GetDetailsOf($_,$bitrateAttribute)
 } | Select-Object -Property Name,Size,DateCreated,Length,BitRate
}

Now the downside of using the Shell.Application object is that the index number for the attributes can be different in between language packs and Windows versions. The code here has been verified to work on En-Us but for other locales changes to the code will have to be made.

For more information in regards to the Shell.Application object please refer to the following MSDN article:
Shell.Application