#requires -version 2.0 Function Get-CIMFile { <# .Synopsis Get a file object via WMI or CIM. .Description This command uses WMI and the CIM_DataFile class to find a file or files on a computer. The command by default searches drive C: on the local computer for the specified file. You can run this as a background job because these types of queries can take a long time to run. .Parameter Name The name of the file to find. .Parameter Drive The name of the drive to search. The default is C:. You do not need to include the trailing \. .Parameter Computername The name of the computer to search. The default is the local computer. This command has no provision, as written, for alternate credentials. .Parameter AsJob Run the search as a background job. .Example PS C:\> Get-Cimfile hidden.txt .Example PS C:\> Get-Cimfile myapp.dll -comp $computers -asjob .Notes Version : 0.9 Last Updated: 1/28/2013 Author : Jeffery Hicks (@JeffHicks) Read PowerShell: Learn Windows PowerShell 3 in a Month of Lunches Learn PowerShell Toolmaking in a Month of Lunches PowerShell in Depth: An Administrator's Guide **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** .Link http://jdhitsolutions.com/blog/2013/01/find-files-with-wmi-and-powershell .Link Get-WmiObject .Inputs String .Outputs System.Management.ManagementObject#root\cimv2\CIM_DataFile #> [cmdletbinding()] Param( [Parameter(Position=0,Mandatory=$True,HelpMessage="What is the name of the file?")] [ValidateNotNullorEmpty()] [alias("file")] [string]$Name, [ValidateNotNullorEmpty()] [string]$Drive="C:", [ValidateNotNullorEmpty()] [string[]]$Computername=$env:computername, [switch]$AsJob ) <# strip off any trailing characters to drive parameter that might have been passed. #> If ($Drive.Length -gt 2) { $Drive=$Drive.Substring(0,2) } Write-Verbose "Searching for $Name on Drive $Drive on computer $Computername." <# Normally you might think to simply split the name on the . character. But you might have a filename like myfile.v2.dll so that won't work. In a case like this the extension would be everything after the last . and the filename everything before. So instead I'll use the substring method to "split" the filename string. #> #get the index of the last . $index = $name.LastIndexOf(".") #get the first part of the name $filename=$Name.Substring(0,$index) #get the last part of the name $extension=$name.Substring($index+1) $filter = "Filename='$filename' AND extension='$extension' AND Drive='$drive'" Write-Verbose $filter #get all instances of the file and write the WMI object to the pipeline Get-WmiObject -Class CIM_Datafile -Filter $filter -ComputerName $Computername -Asjob:$AsJob } #end Get-CIMFile