{"id":7992,"date":"2021-01-08T10:39:15","date_gmt":"2021-01-08T15:39:15","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=7992"},"modified":"2021-01-08T10:42:31","modified_gmt":"2021-01-08T15:42:31","slug":"answering-the-cim-directory-challenge","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/","title":{"rendered":"Answering the CIM Directory Challenge"},"content":{"rendered":"\n<div class=\"wp-block-image is-style-default\"><figure class=\"alignleft size-large\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.png\"><img loading=\"lazy\" decoding=\"async\" width=\"138\" height=\"160\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.png\" alt=\"folder with document\" class=\"wp-image-7994\"\/><\/a><\/figure><\/div>\n\n\n\n<p>The last Iron Scripter challenge of 2020 <a href=\"https:\/\/ironscripter.us\/a-cim-ple-powershell-challenge\" target=\"_blank\" rel=\"noreferrer noopener\">was a big one<\/a>. 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, but rather to push the limits of your PowerShell scripting and tool-making ability. This is a terrific challenge in that regard.<\/p>\n\n\n\n<p>The goal was to get a directory listing of files and folders using WMI\/CIM . This could be a potential alternative to a traditional filesystem listing. As you'll see, it is possible to accomplish this task. However, not every file and\/or folder is registered and I haven't discovered a consistent reason. This means there's no guarantee that when I run my code that I am getting a definitive answer. But the goal is to learn so I'm OK with that. Let's dig in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Win32_Directory<\/h2>\n\n\n\n<p>The basis of a solution begins with the Win32_Directory WMI class. I'll filter for a single folder to get an idea of what it looks like.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-CimInstance\u00a0win32_directory\u00a0-Filter\u00a0\"Name='C:\\\\Work\\\\samples'\"\u00a0|\u00a0Tee-Object\u00a0-Variable\u00a0d<\/code><\/pre>\n\n\n\n<p>You can also use Get-CimClass to examine the class properties.  Remember that with WMI\/CIM filters to use the legacy operators like the = sign. The \\ is a special character and needs to be escaped.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"145\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory-1024x145.png\" alt=\"win32_directory\" class=\"wp-image-7995\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory-1024x145.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory-300x42.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory-768x109.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory-1536x217.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory-2048x290.png 2048w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/win32_directory-850x120.png 850w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Now for the fun part. In WMI, most everything is related. Use the Get-CimAssociatedInstance command to retrieve related objects. You can limit what type of results by filtering on a class name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"149\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2-1024x149.png\" alt=\"associated win32_directory\" class=\"wp-image-7998\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2-1024x149.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2-300x44.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2-768x112.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2-1536x224.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2-850x124.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/assocated-directory2.png 2006w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>For my purpose, l only want to see immediate child directories, so I'll modify the code to filter out the parent.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$d\u00a0|\u00a0Get-CimAssociatedInstance\u00a0-ResultClassName\u00a0Win32_Directory\u00a0|\u00a0\nWhere-Object\u00a0{\u00a0(Split-Path\u00a0$_.name)\u00a0-eq\u00a0$d.name\u00a0}\u00a0|\nSelect-Object\u00a0Hidden,\u00a0Archive,\u00a0System,\u00a0Compressed,\u00a0Writeable,\u00a0Encrypted,\u00a0LastModified,\u00a0Name,\u00a0Path<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CIM_DataFile<\/h2>\n\n\n\n<p>Now that I have a way to get folders, I need to find files. This will use the CIM_DataFile class. I can use the same technique.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$d\u00a0|\u00a0Get-CimAssociatedInstance\u00a0-ResultClassName\u00a0CIM_DATAFile\u00a0|\u00a0Select-Object\u00a0-First\u00a01\u00a0-Property\u00a0*<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"505\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile-1024x505.png\" alt=\"CIM_Datafile\" class=\"wp-image-7999\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile-1024x505.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile-300x148.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile-768x379.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile-1536x757.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile-850x419.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cim_datafile.png 1716w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Again, I can also use Get-CimClass to discover properties, but often it helps to see a real-world example.<\/p>\n\n\n\n<p>Part of the challenge is to duplicate the output from Get-ChildItem so something like this is a good proof-of-concept.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$d\u00a0|\u00a0Get-CimAssociatedInstance\u00a0-ResultClassName\u00a0CIM_DATAFile\u00a0|\nFormat-Table\u00a0-GroupBy\u00a0@{Name\u00a0=\u00a0\"Path\";\u00a0Expression\u00a0=\u00a0{\u00a0Resolve-Path\u00a0(Join-Path\u00a0-Path\u00a0$_.drive\u00a0-ChildPath\u00a0$_.Path)\u00a0}\u00a0}\u00a0-Property\u00a0\u00a0Hidden,\u00a0Archive,\u00a0System,\u00a0LastModified,\u00a0FileSize,\u00a0Name<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"265\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing-1024x265.png\" alt=\"\" class=\"wp-image-8000\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing-1024x265.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing-300x78.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing-768x199.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing-1536x397.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing-2048x530.png 2048w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/sample-listing-850x220.png 850w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Custom Class<\/h2>\n\n\n\n<p>Now that I know how to retrieve files and folders, I want an easier way to display them, without having to rely on cumbersome Select-Object syntax. My answer is to create PowerShell classes.<\/p>\n\n\n\n<pre title=\"My Cim Classses\" class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Class\u00a0cimFolder\u00a0{\n\u00a0\u00a0\u00a0\u00a0[string]$FullName\n\u00a0\u00a0\u00a0\u00a0[string]$Name\n\u00a0\u00a0\u00a0\u00a0[bool]$Archive\n\u00a0\u00a0\u00a0\u00a0[bool]$Compressed\n\u00a0\u00a0\u00a0\u00a0[DateTime]$CreationDate\n\u00a0\u00a0\u00a0\u00a0[string]$Computername\n\u00a0\u00a0\u00a0\u00a0[string]$Drive\n\u00a0\u00a0\u00a0\u00a0[bool]$Encrypted\n\u00a0\u00a0\u00a0\u00a0[bool]$Hidden\n\u00a0\u00a0\u00a0\u00a0[DateTime]$LastAccessed\n\u00a0\u00a0\u00a0\u00a0[DateTime]$LastModified\n\u00a0\u00a0\u00a0\u00a0[string]$Path\n\u00a0\u00a0\u00a0\u00a0[bool]$Readable\n\u00a0\u00a0\u00a0\u00a0[bool]$System\n\u00a0\u00a0\u00a0\u00a0[bool]$Writeable\n\u00a0\u00a0\u00a0\u00a0[string]$Mode\n}\n\nClass\u00a0cimFile\u00a0{\n\u00a0\u00a0\u00a0\u00a0[string]$FullName\n\u00a0\u00a0\u00a0\u00a0[string]$Name\n\u00a0\u00a0\u00a0\u00a0[bool]$Archive\n\u00a0\u00a0\u00a0\u00a0[bool]$Compressed\n\u00a0\u00a0\u00a0\u00a0[DateTime]$CreationDate\n\u00a0\u00a0\u00a0\u00a0[string]$Computername\n\u00a0\u00a0\u00a0\u00a0[string]$Drive\n\u00a0\u00a0\u00a0\u00a0[bool]$Encrypted\n\u00a0\u00a0\u00a0\u00a0[bool]$Hidden\n\u00a0\u00a0\u00a0\u00a0[int64]$FileSize\n\u00a0\u00a0\u00a0\u00a0[DateTime]$LastAccessed\n\u00a0\u00a0\u00a0\u00a0[DateTime]$LastModified\n\u00a0\u00a0\u00a0\u00a0[string]$Path\n\u00a0\u00a0\u00a0\u00a0[bool]$Readable\n\u00a0\u00a0\u00a0\u00a0[bool]$System\n\u00a0\u00a0\u00a0\u00a0[bool]$Writeable\n\u00a0\u00a0\u00a0\u00a0[string]$Mode\n}\n<\/code><\/pre>\n\n\n\n<p>Essentially, these are clones of the original WMI classes. Instead of defining a constructor, I'll write a PowerShell function that will \"convert\" the original WMI class into my custom class.<\/p>\n\n\n\n<pre title=\"New-CimFile\" class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function\u00a0New-CimFile\u00a0{\n\u00a0\u00a0\u00a0\u00a0[cmdletbinding()]\n\u00a0\u00a0\u00a0\u00a0Param(\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[Parameter(<em>Position<\/em>\u00a0=\u00a00,\u00a0<em>Mandatory<\/em>,\u00a0<em>ValueFromPipeline<\/em>)]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[object]$CimObject\n\u00a0\u00a0\u00a0\u00a0)\n\u00a0\u00a0\u00a0\u00a0Begin\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$properties\u00a0=\u00a0[CimFile].DeclaredProperties.Name\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0Process\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$file\u00a0=\u00a0[CimFile]::New()\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0foreach\u00a0($item\u00a0in\u00a0$properties)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$file.$item\u00a0=\u00a0$CimObject.$item\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$file.name\u00a0=\u00a0Split-Path\u00a0-Path\u00a0$CimObject.caption\u00a0-Leaf\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$file.fullname\u00a0=\u00a0$CimObject.Caption\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$file.computername\u00a0=\u00a0$CimObject.CSName\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$file.mode\u00a0=\u00a0Get-Mode\u00a0$CimObject\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$file\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0End\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#not\u00a0used<\/em>\n\u00a0\u00a0\u00a0\u00a0}\n}<\/code><\/pre>\n\n\n\n<p>I also wrote a helper function to get the file mode.<\/p>\n\n\n\n<pre title=\"Get-Mode\" class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function\u00a0Get-Mode\u00a0{\n\u00a0\u00a0\u00a0\u00a0[cmdletbinding()]\n\u00a0\u00a0\u00a0\u00a0param([object]$CimObject)\n\n\u00a0\u00a0\u00a0\u00a0<em>#\u00a0use\u00a0the\u00a0ternary\u00a0operator\u00a0to\u00a0simplify\u00a0the\u00a0code,<\/em>\n\u00a0\u00a0\u00a0\u00a0<em>#\u00a0although\u00a0this\u00a0will\u00a0require\u00a0PowerShell\u00a07.x<\/em>\n\u00a0\u00a0\u00a0\u00a0$dir\u00a0=\u00a0$CimObject.CimClass.Cimclassname\u00a0-match\u00a0'Directory'\u00a0?\u00a0\"d\"\u00a0:\u00a0\"-\"\n\u00a0\u00a0\u00a0\u00a0$archive\u00a0=\u00a0$CimObject.archive\u00a0?\u00a0\"a\"\u00a0:\u00a0\"-\"\n\u00a0\u00a0\u00a0\u00a0$ro\u00a0=\u00a0$CimObject.writeable\u00a0?\u00a0\"-\"\u00a0:\u00a0\"r\"\n\u00a0\u00a0\u00a0\u00a0$system\u00a0=\u00a0$CimObject.System\u00a0?\u00a0\"s\"\u00a0:\u00a0\"-\"\n\u00a0\u00a0\u00a0\u00a0$hidden\u00a0=\u00a0$CimObject.Hidden\u00a0?\u00a0\"h\"\u00a0:\u00a0\"-\"\n\n\u00a0\u00a0\u00a0\u00a0\"{0}{1}{2}{3}{4}\"\u00a0-f\u00a0$Dir,\u00a0$Archive,\u00a0$RO,\u00a0$Hidden,\u00a0$System\n}<\/code><\/pre>\n\n\n\n<p>Note that this function uses the ternary operator so it will only run in PowerShell 7. There's no real reason to use the operator other than I wanted an excuse to try it out. With these elements loaded into my PowerShell session, I can verify the concept.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$d\u00a0|\u00a0Get-CimAssociatedInstance\u00a0-ResultClassName\u00a0CIM_DATAFile\u00a0|\u00a0New-CimFile<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cimFile.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"625\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cimFile-1024x625.png\" alt=\"\" class=\"wp-image-8001\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cimFile-1024x625.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cimFile-300x183.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cimFile-768x468.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cimFile-850x518.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/cimFile.png 1233w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>I can do a similar process for directory objects. Finally, I can put it all together in a single command.<\/p>\n\n\n\n<pre title=\"Get-CimFolder\" class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function\u00a0Get-CimFolder\u00a0{\n\u00a0\u00a0\u00a0\u00a0[cmdletbinding(<em>DefaultParameterSetName<\/em>\u00a0=\u00a0\"computer\")]\n\u00a0\u00a0\u00a0\u00a0[alias(\"cdir\")]\n\u00a0\u00a0\u00a0\u00a0[OutputType(\"cimFolder\",\u00a0\"cimFile\")]\n\u00a0\u00a0\u00a0\u00a0Param(\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[Parameter(<em>Position<\/em>\u00a0=\u00a00,\u00a0<em>HelpMessage<\/em>\u00a0=\u00a0\"Enter\u00a0the\u00a0folder\u00a0path.\u00a0Don't\u00a0include\u00a0the\u00a0trailing\u00a0\\.\")]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[ValidateNotNullorEmpty()]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[string]$Path\u00a0=\u00a0\".\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[switch]$Recurse,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[Parameter(<em>ParameterSetName<\/em>\u00a0=\u00a0\"computer\")]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[ValidateNotNullOrEmpty()]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[alias(\"cn\")]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[string]$Computername\u00a0=\u00a0$ENV:Computername,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[Parameter(<em>ParameterSetName<\/em>\u00a0=\u00a0\"session\")]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[ValidateNotNullOrEmpty()]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[Microsoft.Management.Infrastructure.CimSession]$CimSession\n\u00a0\u00a0\u00a0\u00a0)\n\u00a0\u00a0\u00a0\u00a0Begin\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Starting\u00a0$($myinvocation.MyCommand)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$cimParams\u00a0=\u00a0@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Classname\u00a0\u00a0=\u00a0\"win32_directory\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Filter\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0\"\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CimSession\u00a0=\u00a0\"\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#begin<\/em>\n\u00a0\u00a0\u00a0\u00a0Process\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#convert\u00a0Path\u00a0to\u00a0a\u00a0file\u00a0system\u00a0path<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($path\u00a0-match\u00a0'\\\\$')\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Stripping\u00a0off\u00a0a\u00a0trailing\u00a0slash\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$path\u00a0=\u00a0$path\u00a0-replace\u00a0\"\\\\$\",\u00a0\"\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Try\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$cpath\u00a0=\u00a0Convert-Path\u00a0-Path\u00a0$path\u00a0-ErrorAction\u00a0Stop\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#escape\u00a0any\u00a0\\\u00a0in\u00a0the\u00a0path<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$rPath\u00a0=\u00a0$cpath.replace(\"\\\",\u00a0\"\\\\\")\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$cimParams.Filter\u00a0=\u00a0\"Name='$rpath'\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Using\u00a0query\u00a0$($cimparams.filter)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Catch\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Warning\u00a0\"Can't\u00a0validate\u00a0the\u00a0path\u00a0$path.\u00a0$($_.Exception.Message)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#bail\u00a0out<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($pscmdlet.ParameterSetName\u00a0-eq\u00a0'computer')\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Try\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$cimSession\u00a0=\u00a0New-CimSession\u00a0-ComputerName\u00a0$computername\u00a0-ErrorAction\u00a0Stop\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$tmpSession\u00a0=\u00a0$True\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Catch\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Throw\u00a0$_\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Else\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Using\u00a0an\u00a0existing\u00a0CIMSession\u00a0to\u00a0$($cimsession.computername)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$tmpSession\u00a0=\u00a0$False\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$cimParams.Cimsession\u00a0=\u00a0$CimSession\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Getting\u00a0$cpath\u00a0on\u00a0$($cimsession.computername)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$main\u00a0=\u00a0Get-CimInstance\u00a0@cimParams\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#filter\u00a0out\u00a0the\u00a0parent\u00a0folder<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$main\u00a0|\u00a0Get-CimAssociatedInstance\u00a0-ResultClassName\u00a0Win32_Directory\u00a0|\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Where-Object\u00a0{\u00a0(Split-Path\u00a0$_.name)\u00a0-eq\u00a0$main.name\u00a0}\u00a0|\u00a0New-CimFolder\u00a0-outvariable\u00a0cf\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$main\u00a0|\u00a0Get-CimAssociatedInstance\u00a0-ResultClassName\u00a0CIM_DATAFile\u00a0|\u00a0New-Cimfile\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($cf\u00a0-AND\u00a0$recurse)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Recursing...\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0foreach\u00a0($fldr\u00a0in\u00a0$cf.fullname)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0$fldr\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Get-CimFolder\u00a0-path\u00a0$Fldr\u00a0-CimSession\u00a0$cimSession\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($cimSession\u00a0-AND\u00a0$tmpSession)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Remove\u00a0the\u00a0temporary\u00a0session\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Remove-CimSession\u00a0$cimSession\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#Process<\/em>\n\u00a0\u00a0\u00a0\u00a0End\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Ending\u00a0$($myinvocation.MyCommand)\"\n\u00a0\u00a0\u00a0\u00a0}\n}<\/code><\/pre>\n\n\n\n<p>Because I'm using the CIM cmdlets, I have an option to query a folder on a remote machine, which was also part of the challenge. My function uses parameter sets to support connecting by computer name and by CIMSession.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customizing the Results<\/h2>\n\n\n\n<p>If you try the code, you'll see that the results are the full object with all the properties. Not necessarily easy to read and certainly not meeting the challenge objectives. One of the reasons for creating my own unique class is that I can define custom type extensions, such as a set of default properties.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Update-TypeData\u00a0-TypeName\u00a0cimFolder\u00a0-DefaultDisplayPropertySet\u00a0Mode,\u00a0LastModified,\u00a0Size,\u00a0Name\u00a0-Force\nUpdate-TypeData\u00a0-TypeName\u00a0cimFile\u00a0-MemberType\u00a0AliasProperty\u00a0-MemberName\u00a0Size\u00a0-Value\u00a0FileSize\u00a0-Force\nUpdate-TypeData\u00a0-TypeName\u00a0cimFile\u00a0-DefaultDisplayPropertySet\u00a0Mode,\u00a0LastModified,\u00a0Size,\u00a0Name\u00a0-Force<\/code><\/pre>\n\n\n\n<p>This cleans up the output considerably.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"481\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder-1024x481.png\" alt=\"\" class=\"wp-image-8002\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder-1024x481.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder-300x141.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder-768x361.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder-850x399.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder.png 1246w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>But defined objects can also have custom formatting files. I created one using my trusty <a href=\"http:\/\/bit.ly\/31SGo5o\" target=\"_blank\" rel=\"noreferrer noopener\">New-PSFormatXML<\/a> command.<\/p>\n\n\n\n<pre title=\"cimfilefolder.format.ps1xml\" class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;?xml\u00a0version=\"1.0\"\u00a0encoding=\"UTF-8\"?>\n<em>&lt;!--<\/em>\n<em>Format\u00a0type\u00a0data\u00a0generated\u00a010\/21\/2020\u00a017:19:32\u00a0by\u00a0PROSPERO\\Jeff<\/em>\n<em>This\u00a0file\u00a0was\u00a0created\u00a0using\u00a0the\u00a0New-PSFormatXML\u00a0command\u00a0that\u00a0is\u00a0part<\/em>\n<em>of\u00a0the\u00a0PSScriptTools\u00a0module.<\/em>\n<em>https:\/\/github.com\/jdhitsolutions\/PSScriptTools<\/em>\n\n<em>This\u00a0file\u00a0will\u00a0only\u00a0work\u00a0properly\u00a0in\u00a0PowerShell\u00a07<\/em>\n<em>--><\/em>\n&lt;Configuration>\n\u00a0\u00a0&lt;SelectionSets>\n\u00a0\u00a0\u00a0\u00a0&lt;SelectionSet>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Name>cimFileTypes&lt;\/Name>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Types>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TypeName>cimFolder&lt;\/TypeName>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TypeName>cimFile&lt;\/TypeName>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/Types>\n\u00a0\u00a0\u00a0\u00a0&lt;\/SelectionSet>\n\u00a0\u00a0&lt;\/SelectionSets>\n\u00a0\u00a0&lt;ViewDefinitions>\n\u00a0\u00a0\u00a0\u00a0&lt;View>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Name>default&lt;\/Name>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;ViewSelectedBy>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;SelectionSetName>cimFileTypes&lt;\/SelectionSetName>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/ViewSelectedBy>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;GroupBy>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>&lt;!--<\/em>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0You\u00a0can\u00a0also\u00a0use\u00a0a\u00a0scriptblock\u00a0to\u00a0define\u00a0a\u00a0custom\u00a0property\u00a0name.<\/em>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0You\u00a0must\u00a0have\u00a0a\u00a0Label\u00a0tag.<\/em>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;ScriptBlock>$_.machinename.toUpper()&lt;\/ScriptBlock><\/em>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Label>Computername&lt;\/Label><\/em>\n\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Use\u00a0&lt;Label>\u00a0to\u00a0set\u00a0the\u00a0displayed\u00a0value.<\/em>\n<em>--><\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Label>Path&lt;\/Label>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;ScriptBlock>Join-Path\u00a0-path\u00a0$_.drive\u00a0-childpath\u00a0$_.path\u00a0\u00a0&lt;\/ScriptBlock>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/GroupBy>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableControl>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>&lt;!--Delete\u00a0the\u00a0AutoSize\u00a0node\u00a0if\u00a0you\u00a0want\u00a0to\u00a0use\u00a0the\u00a0defined\u00a0widths.<\/em>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;AutoSize\u00a0\/>--><\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableHeaders>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Label>Mode&lt;\/Label>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Width>6&lt;\/Width>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Alignment>left&lt;\/Alignment>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Label>LastModified&lt;\/Label>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Width>24&lt;\/Width>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Alignment>right&lt;\/Alignment>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Label>Size&lt;\/Label>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Width>14&lt;\/Width>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Alignment>right&lt;\/Alignment>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Label>Name&lt;\/Label>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>&lt;!--\u00a0\u00a0&lt;Width>10&lt;\/Width>\u00a0--><\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;Alignment>left&lt;\/Alignment>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnHeader>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableHeaders>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableRowEntries>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableRowEntry>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnItems>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>&lt;!--<\/em>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0By\u00a0default\u00a0the\u00a0entries\u00a0use\u00a0property\u00a0names,\u00a0but\u00a0you\u00a0can\u00a0replace\u00a0them\u00a0with\u00a0scriptblocks.<\/em>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;ScriptBlock>$_.foo\u00a0\/1mb\u00a0-as\u00a0[int]&lt;\/ScriptBlock><\/em>\n<em>--><\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;PropertyName>Mode&lt;\/PropertyName>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;PropertyName>LastModified&lt;\/PropertyName>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;PropertyName>Size&lt;\/PropertyName>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;ScriptBlock>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>&lt;!--\u00a0show\u00a0Directories,\u00a0hidden,\u00a0compressed\u00a0or\u00a0encrypted\u00a0files\u00a0in\u00a0color\u00a0using\u00a0ANSI--><\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($_.GetType().Name\u00a0-eq\u00a0'cimFolder')\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"`e[38;5;228m$($_.name)`e[0m\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0elseif\u00a0($_.Encrypted\u00a0-OR\u00a0$_.compressed)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"`e[38;5;201m$($_.name)`e[0m\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0elseif\u00a0($_.Hidden)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"`e[38;5;105m$($_.name)`e[0m\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$_.name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/ScriptBlock>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnItem>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableColumnItems>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableRowEntry>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableRowEntries>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/TableControl>\n\u00a0\u00a0\u00a0\u00a0&lt;\/View>\n\u00a0\u00a0&lt;\/ViewDefinitions>\n&lt;\/Configuration><\/code><\/pre>\n\n\n\n<p>The file uses scriptblocks to display certain files in color, which was yet another challenge task. My code uses ANSI escape sequences for PowerShell 7.  Once I load the file with Update-FormatData, look at the amazing difference.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"429\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2-1024x429.png\" alt=\"formatted output\" class=\"wp-image-8003\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2-1024x429.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2-300x126.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2-768x322.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2-1536x643.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2-850x356.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-cimfolder2.png 1617w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Module<\/h2>\n\n\n\n<p>As you can see, there are a lot of moving parts to this solution. I decided to take the extra step and pull everything together into a module called CimFolder. You can find the module on GitHub at <a href=\"https:\/\/github.com\/jdhitsolutions\/CimFolder\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/jdhitsolutions\/CimFolder<\/a>. The module is a proof-of-concept and not necessarily production-ready code so I have no plans to publish it to the PowerShell Gallery. <\/p>\n\n\n\n<p>There is also a potential for incomplete results. Not every file and folder is registered with WMI. In my testing I have come across folders that my command fails to enumerate, yet Get-ChildItem works just fine. I have yet to discover a consistent explanation or fix which is why I wouldn't rely on my code for production. However, I hope how and why I put things together has been educational.<\/p>\n\n\n\n<p>The module has the latest updates to the code samples in this blog post. But I have to say, I am quite happy with the results.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/jdhitsolutions\/CimFolder\/main\/images\/get-cimfolder.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/jdhitsolutions\/CimFolder\/main\/images\/cimsession.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>I hope you'll browse through the code for ideas and inspiration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Expand Your Skills<\/h2>\n\n\n\n<p>I hope you'll keep an eye on the<a href=\"https:\/\/ironscripter.us\" target=\"_blank\" rel=\"noreferrer noopener\"> Iron Scripter<\/a> site and try your hand at the challenges. The challenges are open-ended so I encourage you to browse the site and tackle a few of them.<\/p>\n\n\n\n<p>If you like learning from challenges, I suggest getting a copy of T<a href=\"https:\/\/leanpub.com\/psprimer\" target=\"_blank\" rel=\"noreferrer noopener\">he PowerShell Practice Primer.<\/a> This book contains 100 practice problems that begin on a very easy level and increase in difficulty.  Answers should be no more than a few PowerShell expressions you would run in the console. These are not scripting challenges.<\/p>\n\n\n\n<p>Or to take your scripting skills to the next level, take a look at <a href=\"https:\/\/leanpub.com\/powershell-scripting-toolmaking\/\" target=\"_blank\" rel=\"noreferrer noopener\">The PowerShell Scripting and Toolmaking Book<\/a> which is also on LeanPub.<\/p>\n\n\n\n<p>In any event, the best way to improve your PowerShell skills is to simply continue to use them every day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last Iron Scripter challenge of 2020 was a big one. If you didn&#8217;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&#8217;t to produce a production-ready PowerShell tool, but rather to push the&#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":"New on the blog: Answering the CIM Directory Challenge #PowerShell","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":[4,8,19],"tags":[387,516,534,540,547],"class_list":["post-7992","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","category-wmi","tag-cim","tag-classes","tag-powershell","tag-scripting","tag-wmi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Answering the CIM Directory Challenge &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"This is my solution for an Iron Scripter PowerShell challenge to display files and folders using the CIM cmdlets. Works locally and remote.\" \/>\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\/powershell\/7992\/answering-the-cim-directory-challenge\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Answering the CIM Directory Challenge &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"This is my solution for an Iron Scripter PowerShell challenge to display files and folders using the CIM cmdlets. Works locally and remote.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-08T15:39:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-08T15:42:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.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=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Answering the CIM Directory Challenge\",\"datePublished\":\"2021-01-08T15:39:15+00:00\",\"dateModified\":\"2021-01-08T15:42:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/\"},\"wordCount\":969,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/folder.png\",\"keywords\":[\"CIM\",\"Classes\",\"PowerShell\",\"Scripting\",\"WMI\"],\"articleSection\":[\"PowerShell\",\"Scripting\",\"WMI\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/\",\"name\":\"Answering the CIM Directory Challenge &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/folder.png\",\"datePublished\":\"2021-01-08T15:39:15+00:00\",\"dateModified\":\"2021-01-08T15:42:31+00:00\",\"description\":\"This is my solution for an Iron Scripter PowerShell challenge to display files and folders using the CIM cmdlets. Works locally and remote.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/folder.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/folder.png\",\"width\":138,\"height\":160},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7992\\\/answering-the-cim-directory-challenge\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Answering the CIM Directory Challenge\"}]},{\"@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":"Answering the CIM Directory Challenge &#8226; The Lonely Administrator","description":"This is my solution for an Iron Scripter PowerShell challenge to display files and folders using the CIM cmdlets. Works locally and remote.","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\/powershell\/7992\/answering-the-cim-directory-challenge\/","og_locale":"en_US","og_type":"article","og_title":"Answering the CIM Directory Challenge &#8226; The Lonely Administrator","og_description":"This is my solution for an Iron Scripter PowerShell challenge to display files and folders using the CIM cmdlets. Works locally and remote.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-01-08T15:39:15+00:00","article_modified_time":"2021-01-08T15:42:31+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.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":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Answering the CIM Directory Challenge","datePublished":"2021-01-08T15:39:15+00:00","dateModified":"2021-01-08T15:42:31+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/"},"wordCount":969,"commentCount":3,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.png","keywords":["CIM","Classes","PowerShell","Scripting","WMI"],"articleSection":["PowerShell","Scripting","WMI"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/","name":"Answering the CIM Directory Challenge &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.png","datePublished":"2021-01-08T15:39:15+00:00","dateModified":"2021-01-08T15:42:31+00:00","description":"This is my solution for an Iron Scripter PowerShell challenge to display files and folders using the CIM cmdlets. Works locally and remote.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/folder.png","width":138,"height":160},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7992\/answering-the-cim-directory-challenge\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Answering the CIM Directory Challenge"}]},{"@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":6082,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6082\/searching-for-a-cim-wmi-class-with-powershell\/","url_meta":{"origin":7992,"position":0},"title":"Searching for a CIM\/WMI Class with PowerShell","author":"Jeffery Hicks","date":"September 18, 2018","format":false,"excerpt":"I got a question on Twitter about an older function I has posted to get antivirus information via WMI. The function continues to work fine with Windows 10, although there's always room for improvement. However, the question was that the function did not seem to work when querying a server\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\/2018\/09\/image_thumb.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2018\/09\/image_thumb.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2018\/09\/image_thumb.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2018\/09\/image_thumb.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3661,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3661\/creating-cim-scripts-without-scripting\/","url_meta":{"origin":7992,"position":1},"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":133,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/133\/techmentor-just-around-the-corner\/","url_meta":{"origin":7992,"position":2},"title":"Techmentor Just Around the Corner","author":"Jeffery Hicks","date":"March 4, 2008","format":false,"excerpt":"The registration deadline for the first Techmentor conference of the year is almost upon us. I'll be doing sessions on using Powershell to manage Active Directory, PowerShell and WMI, Logon Scripts and more. Plus, I'm always happy to hang out and chat. I always have a great time. Hope to\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":43,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/43\/more-free-scripting-tools\/","url_meta":{"origin":7992,"position":3},"title":"More free scripting tools","author":"Jeffery Hicks","date":"August 16, 2006","format":false,"excerpt":"SAPIEN Technologies has released some free tools for scripters at http:\/\/www.primalscript.com\/freetools\/. One of them is the PowerShell help viewer I discussed a few posts back. This is great when you're in PowerShell and can't remember a cmdlet's syntax.They also have a nifty WMI explorer. This tool lets you browse all\u2026","rel":"","context":"In &quot;Scripting&quot;","block_context":{"text":"Scripting","link":"https:\/\/jdhitsolutions.com\/blog\/category\/scripting\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1454,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/1454\/teched-atlanta-managing-the-registry-with-powershell\/","url_meta":{"origin":7992,"position":4},"title":"TechEd Atlanta &#8211; Managing the Registry with PowerShell","author":"Jeffery Hicks","date":"May 23, 2011","format":false,"excerpt":"My second TechEd talk was about managing the registry with Windows PowerShell. If you were in the session you know that I stressed heavily using the PowerShell provider and cmdlets. For remote computers, leverage PowerShell's remoting infrastructure. But I also discussed using the \"raw\" .NET classes as well as WMI\u2026","rel":"","context":"In &quot;Conferences&quot;","block_context":{"text":"Conferences","link":"https:\/\/jdhitsolutions.com\/blog\/category\/conferences\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":37,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/37\/get-active-directory-user-information-in-powershell\/","url_meta":{"origin":7992,"position":5},"title":"Get Active Directory User Information in PowerShell","author":"Jeffery Hicks","date":"July 5, 2006","format":false,"excerpt":"One feature that PowerShell will likely be missing when it first ships is solid support for ADSI and working with Active Directory. You can use .NET DirectoryEntry objects but it feels more like programming and less like scripting. Another option for working with Active Directory in PowerShell is to use\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":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7992","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=7992"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7992\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}