#requires -version 3.0 Function Get-CIMFile { <# .Synopsis Get a file object using CIM. .Description This command uses the Get-CIMInstance 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. The wildcard character (*) is permitted anywhere in the filename. You can search one or more remote computers by computername or CIM sessions. .Parameter Name The name of the file to find. Wildcards (*) in the name are permitted but they will make the search run a little longer. .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. .Parameter CimSession Connect to remote computers via a Cimsession object. .Example PS C:\> Get-Cimfile hidden.txt .Example PS C:\> Get-Cimfile myapp.dll -comp $computers .Example PS C:\> $sess = New-CimSession $computers PS C:\> Get-Cimfile myapp.dll -cimsession $sess | Select PSComputername,Name,Filesize .Notes Version : 3.0 Last Updated: 02/06/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/02/find-files-with-powershell-3-0/ .Link Get-CIMInstance New-CimSession .Inputs String .Outputs Microsoft.Management.Infrastructure.CimInstance#root\cimv2\CIM_DataFile #> [cmdletbinding(DefaultParameterSetName="Computername")] Param( [Parameter(Position=0,Mandatory=$True,HelpMessage="What is the name of the file?")] [ValidateNotNullorEmpty()] [alias("file")] [string]$Name, [ValidatePattern("^[a-zA-Z]:$")] [string]$Drive="C:", [Parameter(ParameterSetName="Computername")] [ValidateNotNullorEmpty()] [string[]]$Computername=$env:computername, [Parameter(ParameterSetName="CIMSession")] [ValidateNotNullorEmpty()] [Microsoft.Management.Infrastructure.CimSession[]]$CimSession ) Write-Verbose "Starting $($MyInvocation.MyCommand)" Write-Verbose "Parameter set = $($PSCmdlet.ParameterSetName)" #create a hashtable of parameter values that can be splatted to Get-CimInstance $paramHash=@{Classname="CIM_DATAFILE"} Write-Verbose "Searching for $filename on drive $drive" if ($pscmdlet.ParameterSetName -eq "Computername") { Write-Verbose "...on $computername" $paramHash.Add("Computername",$computername) } elseif ($pscmdlet.ParameterSetName -eq "CimSession") { Write-Verbose "...on $Cimsession" $paramHash.Add("CimSession",$cimSession) } else { #this should never happen Write-Verbose "No computername or cimsession specified. Defaulting to local host" #bail out of the function Return } #define default operators $fileOp="=" $extOp="=" <# 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(".") #it is possible the filename doesn't have an extension if ($index -gt 0) { #get the first part of the name $filename=$Name.Substring(0,$index) #get the last part of the name $extension=$name.Substring($index+1) } else { $filename=$Name #will need to use wildcard search for filename when extension is empty $fileop="LIKE" $extension=$null } #if there is * in the filename or extension, replace it with % #and change the comparison operator for the WMI query if ($filename -match "\*" ) { Write-Verbose "Wildcard search on filename" $filename = $filename.Replace("*","%") $fileOp="LIKE" } if ($extension -match "\*") { Write-Verbose "Wildcard search on extension" $extension = $extension.Replace("*","%") $extOp="LIKE" } $filter = "Filename $fileOp '$filename' AND extension $extOp '$extension' AND Drive='$drive'" Write-Verbose $filter #add the filter to the hashtable $paramHash.Add("Filter",$filter) #invoke the command Write-Verbose "Parameter Hash $($paramHash| out-String)" #let's time how long it took $start=Get-Date Get-CimInstance @paramhash $end=Get-Date Write-Verbose "Search completed in $($end-$start)" Write-Verbose "Ending $($MyInvocation.MyCommand)" } #end Get-CIMFile