{"id":2781,"date":"2013-02-07T10:11:45","date_gmt":"2013-02-07T15:11:45","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=2781"},"modified":"2014-02-21T08:10:00","modified_gmt":"2014-02-21T13:10:00","slug":"find-files-with-powershell-3-0","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/","title":{"rendered":"Find Files with PowerShell 3.0"},"content":{"rendered":"<p>My last few articles have looked at using WMI and CIM_DATAFILE class to find files, primarily using Get-WmiObject in PowerShell. But now that we have PowerShell 3.0 at our disposal, we can use the new CIM cmdlets. So I took my most recent version of Get-CIMFile and revised it specifically to use Get-CimInstance. I also took the liberty of adding a few more refinements, some of which you could integrate into previous versions. Here's the new v3 function.<\/p>\n<pre class=\"lang:ps decode:true \" >#Requires -version 3.0\r\n\r\nFunction Get-CIMFile {\r\n&lt;#\r\n.Synopsis\r\nGet a file object using CIM.\r\n.Description\r\nThis command uses the Get-CIMInstance and the CIM_DataFile class to find a \r\nfile or files on a computer. The command by default searches drive C: on the \r\nlocal computer for the specified file. The wildcard character (*) is permitted \r\nanywhere in the filename. \r\n\r\nYou can search one or more remote computers by computername or CIM sessions.\r\n\r\n.Parameter Name\r\nThe name of the file to find. Wildcards (*) in the name are permitted but they\r\nwill make the search run a little longer.\r\n.Parameter Drive\r\nThe name of the drive to search. The default is C:. You do not need to include\r\nthe trailing \\.\r\n.Parameter Computername\r\nThe name of the computer to search. The default is the local computer. \r\n.Parameter CimSession\r\nConnect to remote computers via a Cimsession object.\r\n.Example\r\nPS C:\\&gt; Get-Cimfile hidden.txt\r\n.Example\r\nPS C:\\&gt; Get-Cimfile myapp.dll -comp $computers \r\n.Example\r\nPS C:\\&gt; $sess = New-CimSession $computers\r\nPS C:\\&gt; Get-Cimfile myapp.dll -cimsession $sess | Select PSComputername,Name,Filesize\r\n.Notes\r\nVersion     : 3.0\r\nLast Updated: 02\/06\/2013\r\nAuthor      : Jeffery Hicks (@JeffHicks)\r\n\r\nRead PowerShell:\r\nLearn Windows PowerShell 3 in a Month of Lunches\r\nLearn PowerShell Toolmaking in a Month of Lunches\r\nPowerShell in Depth: An Administrator's Guide\r\n\r\n  ****************************************************************\r\n  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *\r\n  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *\r\n  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *\r\n  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *\r\n  ****************************************************************\r\n.Link\r\nhttp:\/\/jdhitsolutions.com\/blog\/2013\/02\/find-files-with-powershell-3-0\/\r\n.Link\r\nGet-CIMInstance\r\nNew-CimSession\r\n.Inputs\r\nString\r\n.Outputs\r\nMicrosoft.Management.Infrastructure.CimInstance#root\\cimv2\\CIM_DataFile\r\n#&gt;\r\n\r\n[cmdletbinding(DefaultParameterSetName=\"Computername\")]\r\n\r\nParam(\r\n[Parameter(Position=0,Mandatory=$True,HelpMessage=\"What is the name of the file?\")]\r\n[ValidateNotNullorEmpty()]\r\n[alias(\"file\")]\r\n[string]$Name,\r\n[ValidatePattern(\"^[a-zA-Z]:$\")]\r\n[string]$Drive=\"C:\",\r\n[Parameter(ParameterSetName=\"Computername\")]\r\n[ValidateNotNullorEmpty()]\r\n[string[]]$Computername=$env:computername,\r\n[Parameter(ParameterSetName=\"CIMSession\")]\r\n[ValidateNotNullorEmpty()]\r\n[Microsoft.Management.Infrastructure.CimSession[]]$CimSession\r\n)\r\n\r\nWrite-Verbose \"Starting $($MyInvocation.MyCommand)\"\r\nWrite-Verbose \"Parameter set = $($PSCmdlet.ParameterSetName)\"\r\n\r\n#create a hashtable of parameter values that can be splatted to Get-CimInstance\r\n$paramHash=@{Classname=\"CIM_DATAFILE\"}\r\n\r\nWrite-Verbose \"Searching for $filename on drive $drive\"\r\nif ($pscmdlet.ParameterSetName -eq \"Computername\") {\r\n    Write-Verbose \"...on $computername\"\r\n    $paramHash.Add(\"Computername\",$computername)\r\n}\r\nelseif ($pscmdlet.ParameterSetName -eq \"CimSession\")  {\r\n    Write-Verbose \"...on $Cimsession\"\r\n    $paramHash.Add(\"CimSession\",$cimSession)\r\n}\r\nelse {\r\n    #this should never happen\r\n    Write-Verbose \"No computername or cimsession specified. Defaulting to local host\"\r\n    #bail out of the function\r\n    Return\r\n}\r\n\r\n#define default operators\r\n$fileOp=\"=\"\r\n$extOp=\"=\"\r\n\r\n&lt;#\r\nNormally you might think to simply split the name on the . character. But\r\nyou might have a filename like myfile.v2.dll so that won't work. In a case \r\nlike this the extension would be everything after the last . and the filename\r\neverything before.\r\n\r\nSo instead I'll use the substring method to \"split\" the filename string.\r\n#&gt;\r\n\r\n#get the index of the last .\r\n$index = $name.LastIndexOf(\".\")\r\n\r\n#it is possible the filename doesn't have an extension\r\nif ($index -gt 0) {\r\n    #get the first part of the name\r\n    $filename=$Name.Substring(0,$index)\r\n    #get the last part of the name\r\n    $extension=$name.Substring($index+1)\r\n}\r\nelse {\r\n    $filename=$Name\r\n    #will need to use wildcard search for filename when extension is empty\r\n    $fileop=\"LIKE\"\r\n    $extension=$null\r\n}\r\n#if there is * in the filename or extension, replace it with %\r\n#and change the comparison operator for the WMI query\r\nif ($filename -match \"\\*\" ) { \r\n    Write-Verbose \"Wildcard search on filename\"\r\n    $filename = $filename.Replace(\"*\",\"%\")\r\n    $fileOp=\"LIKE\"\r\n}\r\n\r\nif ($extension -match \"\\*\") {\r\n    Write-Verbose \"Wildcard search on extension\"\r\n    $extension = $extension.Replace(\"*\",\"%\")\r\n    $extOp=\"LIKE\"\r\n}\r\n\r\n$filter = \"Filename $fileOp '$filename' AND extension $extOp '$extension' AND Drive='$drive'\"\r\nWrite-Verbose $filter\r\n\r\n#add the filter to the hashtable\r\n$paramHash.Add(\"Filter\",$filter)\r\n\r\n#invoke the command\r\nWrite-Verbose \"Parameter Hash $($paramHash| out-String)\"\r\n\r\n#let's time how long it took\r\n$start=Get-Date  \r\n\r\nGet-CimInstance @paramhash\r\n\r\n$end=Get-Date\r\nWrite-Verbose \"Search completed in $($end-$start)\"\r\nWrite-Verbose \"Ending $($MyInvocation.MyCommand)\"\r\n\r\n} #end Get-CIMFile<\/pre>\n<p>This version let's you search remote computers by name or CIM session. Because they are mutually exclusive options, this function uses parameter sets, defaulting to using a computername. I also added a validation check on the drive name using regular expressions. The function will fail if the value is not a letter followed by a colon.<\/p>\n<p>Another major change was modifying code to search for filenames without an extension.  What if you are looking for a file like README? The WQL query turned out to be more complicated than I imagined. It would be easier if the extension property was NULL, but it isn't. It is a 0 length string. I found that in order to make this work, I needed to create a query like this:<\/p>\n<pre class=\"lang:ps decode:true \" >SELECT * FROM CIM_DATAFILE WHERE Filename LIKE 'readme' AND extension = '' AND Drive='d:'<\/pre>\n<p>So I modified my code to adjust operators and variables that I use to build the filter string.<\/p>\n<pre class=\"lang:ps decode:true \" >#get the index of the last .\r\n$index = $name.LastIndexOf(\".\")\r\n\r\n#it is possible the filename doesn't have an extension\r\nif ($index -gt 0) {\r\n    #get the first part of the name\r\n    $filename=$Name.Substring(0,$index)\r\n    #get the last part of the name\r\n    $extension=$name.Substring($index+1)\r\n}\r\nelse {\r\n    $filename=$Name\r\n    #will need to use wildcard search for filename when extension is empty\r\n    $fileop=\"LIKE\"\r\n    $extension=$null\r\n}\r\n...\r\n$filter = \"Filename $fileOp '$filename' AND extension $extOp '$extension' AND Drive='$drive'\"<\/pre>\n<p>Now I can find files without an extension, or with.<\/p>\n<pre class=\"lang:batch decode:true \" >PS C:\\&gt;  get-cimfile readme -drive d:\r\n\r\nCompressed     : False\r\nEncrypted      : False\r\nSize           : \r\nHidden         : False\r\nName           : d:\\readme\r\nReadable       : True\r\nSystem         : False\r\nVersion        : \r\nWriteable      : True\r\nPSComputerName : SERENITY\r\n\r\nPS C:\\&gt;  get-cimfile readme.txt -drive d:\r\n\r\n\r\nCompressed     : False\r\nEncrypted      : False\r\nSize           : \r\nHidden         : False\r\nName           : d:\\readme.txt\r\nReadable       : True\r\nSystem         : False\r\nVersion        : \r\nWriteable      : True\r\nPSComputerName : SERENITY<\/pre>\n<p>The last major change you'll notice is that I build a hash table of parameter values and then splat it.<\/p>\n<pre class=\"lang:ps decode:true \" >$paramHash=@{Classname=\"CIM_DATAFILE\"}\r\n\r\nWrite-Verbose \"Searching for $filename on drive $drive\"\r\nif ($pscmdlet.ParameterSetName -eq \"Computername\") {\r\n    Write-Verbose \"...on $computername\"\r\n    $paramHash.Add(\"Computername\",$computername)\r\n}\r\nelseif ($pscmdlet.ParameterSetName -eq \"CimSession\")  {\r\n    Write-Verbose \"...on $Cimsession\"\r\n    $paramHash.Add(\"CimSession\",$cimSession)\r\n}\r\n...\r\n#add the filter to the hashtable\r\n$paramHash.Add(\"Filter\",$filter)\r\n...\r\nGet-CimInstance @paramhash<\/pre>\n<p>This is a terrific technique when you are dynamically generating parameters.<\/p>\n<p>Get-CimInstance can be used to query remote computers, assuming they are also running PowerShell 3.0. However, you can also use CIM Sessions which allow you to establish a connection to an older system using the DCOM protocol.<\/p>\n<pre class=\"lang:ps decode:true \" >$sess = New-CimSession jdhit-dc01 -SessionOption (New-CimSessionOption -Protocol Dcom)<\/pre>\n<p>The end result is that I can still use my PowerShell 3.0 function to query a PowerShell 2.0 machine as long as I have a pre-created session.<\/p>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-1024x703.png\" alt=\"get-cimfile3\" width=\"625\" height=\"429\" class=\"aligncenter size-large wp-image-2782\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-1024x703.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-300x206.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-624x428.png 624w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3.png 1137w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>Now I have a very powerful tool that can search just about any computer in my domain.<\/p>\n<p>Oh, one more thing I realized in working on this. Initially I was only paying attention to the file name and version. Then I noticed that the Size property in the default output was always empty. That struck me as odd and not very useful. So I looked at the actual object with Get-Member and it turns out there is a FileSize property which is populated. It looks like the default property set for CIM_DATAFILE uses the Size property, when it should really be FileSize. So keep that in mind as you are working with the results.<\/p>\n<p>Download <a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/Get-CIMFile3.txt\" target=\"_blank\">Get-CIMFile3<\/a> and try it out for yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My last few articles have looked at using WMI and CIM_DATAFILE class to find files, primarily using Get-WmiObject in PowerShell. But now that we have PowerShell 3.0 at our disposal, we can use the new CIM cmdlets. So I took my most recent version of Get-CIMFile and revised it specifically to use Get-CimInstance. I also&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[359,8,19],"tags":[387,32,388,534,540],"class_list":["post-2781","post","type-post","status-publish","format-standard","hentry","category-powershell-3-0","category-scripting","category-wmi","tag-cim","tag-functions","tag-get-ciminstance","tag-powershell","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Find Files with PowerShell 3.0 &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find Files with PowerShell 3.0 &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"My last few articles have looked at using WMI and CIM_DATAFILE class to find files, primarily using Get-WmiObject in PowerShell. But now that we have PowerShell 3.0 at our disposal, we can use the new CIM cmdlets. So I took my most recent version of Get-CIMFile and revised it specifically to use Get-CimInstance. I also...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2013-02-07T15:11:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-02-21T13:10:00+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-1024x703.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Find Files with PowerShell 3.0\",\"datePublished\":\"2013-02-07T15:11:45+00:00\",\"dateModified\":\"2014-02-21T13:10:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/\"},\"wordCount\":455,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/02\\\/get-cimfile3-1024x703.png\",\"keywords\":[\"CIM\",\"functions\",\"Get-CIMInstance\",\"PowerShell\",\"Scripting\"],\"articleSection\":[\"Powershell 3.0\",\"Scripting\",\"WMI\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/\",\"name\":\"Find Files with PowerShell 3.0 &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/02\\\/get-cimfile3-1024x703.png\",\"datePublished\":\"2013-02-07T15:11:45+00:00\",\"dateModified\":\"2014-02-21T13:10:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/02\\\/get-cimfile3.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/02\\\/get-cimfile3.png\",\"width\":1137,\"height\":781},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/2781\\\/find-files-with-powershell-3-0\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Powershell 3.0\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell-3-0\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Find Files with PowerShell 3.0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find Files with PowerShell 3.0 &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/","og_locale":"en_US","og_type":"article","og_title":"Find Files with PowerShell 3.0 &#8226; The Lonely Administrator","og_description":"My last few articles have looked at using WMI and CIM_DATAFILE class to find files, primarily using Get-WmiObject in PowerShell. But now that we have PowerShell 3.0 at our disposal, we can use the new CIM cmdlets. So I took my most recent version of Get-CIMFile and revised it specifically to use Get-CimInstance. I also...","og_url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/","og_site_name":"The Lonely Administrator","article_published_time":"2013-02-07T15:11:45+00:00","article_modified_time":"2014-02-21T13:10:00+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-1024x703.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Find Files with PowerShell 3.0","datePublished":"2013-02-07T15:11:45+00:00","dateModified":"2014-02-21T13:10:00+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/"},"wordCount":455,"commentCount":2,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-1024x703.png","keywords":["CIM","functions","Get-CIMInstance","PowerShell","Scripting"],"articleSection":["Powershell 3.0","Scripting","WMI"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/","url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/","name":"Find Files with PowerShell 3.0 &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3-1024x703.png","datePublished":"2013-02-07T15:11:45+00:00","dateModified":"2014-02-21T13:10:00+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/get-cimfile3.png","width":1137,"height":781},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2781\/find-files-with-powershell-3-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Powershell 3.0","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},{"@type":"ListItem","position":2,"name":"Find Files with PowerShell 3.0"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":7992,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/","url_meta":{"origin":2781,"position":0},"title":"Answering the CIM Directory Challenge","author":"Jeffery Hicks","date":"January 8, 2021","format":false,"excerpt":"The last Iron Scripter challenge of 2020 was a big one. If you didn't get a chance to work on it, see what you can come up with then come back to see my approach. As with many of the challenges, the goal isn't to produce a production-ready PowerShell tool,\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":2935,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2935\/get-ciminstance-from-powershell-2-0\/","url_meta":{"origin":2781,"position":1},"title":"Get CIMInstance from PowerShell 2.0","author":"Jeffery Hicks","date":"April 10, 2013","format":false,"excerpt":"I love the new CIM cmdlets in PowerShell 3.0. Querying WMI is a little faster because the CIM cmdlets query WMI using the WSMAN protocol instead of DCOM. The catch is that remote computers must be running PowerShell 3 which includes the latest version of the WSMAN protocol and the\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"get-ciminstance-error","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/04\/get-ciminstance-error-300x145.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2760,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2760\/find-files-with-wmi-and-powershell\/","url_meta":{"origin":2781,"position":2},"title":"Find Files with WMI and PowerShell","author":"Jeffery Hicks","date":"January 29, 2013","format":false,"excerpt":"Finding files is one of those necessary evils for IT Pros. Sometimes we're searching for a needle in a haystack. And it gets even more complicated when the haystacks are on 10 or 100 or 1000 remote computers. You might think using Get-ChildItem is your only option. Certainly it works,\u2026","rel":"","context":"In &quot;Scripting&quot;","block_context":{"text":"Scripting","link":"https:\/\/jdhitsolutions.com\/blog\/category\/scripting\/"},"img":{"alt_text":"magnifying-glass-text-label-search","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/01\/magnifying-glass-text-label-search-150x150.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3661,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3661\/creating-cim-scripts-without-scripting\/","url_meta":{"origin":2781,"position":3},"title":"Creating CIM Scripts without Scripting","author":"Jeffery Hicks","date":"January 29, 2014","format":false,"excerpt":"When Windows 8 and Windows Server 2012 came out, along with PowerShell 3.0, we got our hands on some terrific technology in the form of the CIM cmdlets. Actually, we got much more than people realize. One of the reasons there was a big bump in the number of shipping\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7835,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7835\/finding-zombie-files-with-powershell\/","url_meta":{"origin":2781,"position":4},"title":"Finding Zombie Files with PowerShell","author":"Jeffery Hicks","date":"October 30, 2020","format":false,"excerpt":"Since this is Halloween weekend in the United States, I thought I'd offer up a PowerShell solution to a scary task - finding zombie files. Ok, maybe these aren't really living dead files, but rather files with a 0-byte length. It is certainly possible that you may intentionally want a\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/datatfile-1.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":2342,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2342\/query-local-administrators-with-cim\/","url_meta":{"origin":2781,"position":5},"title":"Query Local Administrators with CIM","author":"Jeffery Hicks","date":"May 24, 2012","format":false,"excerpt":"Yesterday I posted an article on listing members of the local administrators group with PowerShell and Get-WmiObject. PowerShell 3.0 offers an additional way using the CIM cmdlets. The CIM cmdlets query the same WMI information, except instead of using the traditional RPC\/DCOM connection, these cmdlets utilize PowerShell's remoting endpoint so\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/talkbubble-v3-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2781"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2781\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}