Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
June 13, 2024

Win32_Directory Scripting

In the [last article](https://buttondown.email/behind-the-powershell-pipeline/archive/powershell-scripting-with-cim_datafile/), I introduced you to the `CIM_DataFile` CIM class. I demonstrated how you might use it as an alternative to the file system. You can use a CIM session for remoting and get additional properties.

#create CIMSession to a remote computer

[CimSession]$cs = 'thinkx1-jh'

#query properties

$Properties = "Name","FileSize","LastModified","FileType"

$query= "Select $($Properties -join ',') from CIM_DataFile where Name = 'c:\\scripts\\db.png'"

#update properties for Select-Object

$Properties+= @{Name="ComputerName";Expression={$cs.ComputerName.ToUpper()}}

$cs.QueryInstances("Root/Cimv2","WQL",$query) | Select-Object -Property $Properties
Personally, I find the property names a bit more meaningful.
Name         : c:\scripts\db.png

FileSize     : 13664

LastModified : 7/16/2013 5:42:07 PM

FileType     : PNG Image

ComputerName : THINKX1-JH
As I showed last time, you can get all files from a single directory.
$cs.QueryInstances("Root/Cimv2","WQL","Select * from CIM_DataFile where Path = '\\work\\' AND Drive = 'C:' AND FileSize>=$(5MB)") | Select-Object Name,FileSize,CreationDate,LastModified,FileType | Sort-Object FileSize -descending | Format-Table
Get 5MB work files
figure 1
It is just as easy to get all files from a single location.
[object[]]$a = $cs.QueryInstances("Root/Cimv2","WQL","Select * from CIM_DataFile where Path = '\\scripts\\' AND Drive = 'C:'")
This took 5.2 seconds top get 4166 files remotely. Because of the way the `QueryInstance` method works, I'm specifically casting `$a` as an array . This make it easier to work with the output.
$a | Group Filetype | Sort-Object count -Descending | Select-Object -first 10 -Property Count,Name,

@{Name='TotalSizeMB';Expression={($_.Group | Measure-Object -Property FileSize -Sum).Sum / 1KB}}
file report
figure 2
Where this gets trickier is when you want to recurse through a folder. I want to show you some options for that situation today.
Get a premium subscription for full article and archive access

In the last article, I introduced you to the CIM_DataFile CIM class. I demonstrated how you might use it as an alternative to the file system. You can use a CIM session for remoting and get additional properties.

#create CIMSession to a remote computer
[CimSession]$cs = 'thinkx1-jh'
#query properties
$Properties = "Name","FileSize","LastModified","FileType"
$query= "Select $($Properties -join ',') from CIM_DataFile where Name = 'c:\\scripts\\db.png'"
#update properties for Select-Object
$Properties+= @{Name="ComputerName";Expression={$cs.ComputerName.ToUpper()}}
$cs.QueryInstances("Root/Cimv2","WQL",$query) | Select-Object -Property $Properties

Personally, I find the property names a bit more meaningful.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.