PowerShell Get-WmiObject with a variable in filter

Let’s say you want to use PowerShell’s Get-WmiObject commandlet to retrieve a list of files older than a certain amount of time. Here’s one way to do it.

$date = Get-Date # Current time
$date = $date.AddHours(-1) # Change this to whatever amount of time you are looking for
$date = Get-Date $date -Format G # This format is the format that the WMI commandlet likes

Note how cleverly I put the variable in here The path must have two backslashes

$myFilter = "drive='C:' and path='\\path\\to\\files' and creationdate<='$date'"
The actual query
$files = Get-WmiObject cim_datafile -ComputerName remote_server_name -filter $myFilter
If you want a list of file names that match, try this
$files | % { $_.name }