{"id":4851,"date":"2012-10-09T00:01:00","date_gmt":"2012-10-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/09\/finding-read-only-and-system-files-by-using-powershell-3-0\/"},"modified":"2012-10-09T00:01:00","modified_gmt":"2012-10-09T00:01:00","slug":"finding-read-only-and-system-files-by-using-powershell-3-0","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/finding-read-only-and-system-files-by-using-powershell-3-0\/","title":{"rendered":"Finding Read-Only and System Files by Using PowerShell 3.0"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn how to use new parameters with the enhanced <strong>Get-ChildItem<\/strong> cmdlet in&nbsp;Windows PowerShell 3.0.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. There are a number of really cool new things in Windows PowerShell 3.0. Some of these make my life a lot easier, and yet they are not the &ldquo;big&rdquo; things. I have written about one of my favorite things<span>&mdash;<\/span>and that is the &ldquo;automatic foreach&rdquo; feature that permits me to directly access a property from a collection. I use the feature nearly every day.<\/p>\n<h2><span style=\"font-size: large\">Enhanced file and folder work<\/span><\/h2>\n<p>One new feature that has received virtually no press, is the new switches available for the <strong>Get-ChildItem<\/strong> cmdlet. The new switches permit simplified code. For example, in Windows PowerShell 1.0 and in Windows PowerShell 2.0, if I wanted to get a list of directories, I needed to write code similar to the code shown here.<\/p>\n<p style=\"padding-left: 30px\">dir | where {$_.psiscontainer}<\/p>\n<p style=\"padding-left: 30px\">To find the files (including hidden files) I would need to use either of the following types of commands.<\/p>\n<p style=\"padding-left: 30px\">dir -force | where {!($_.psiscontainer)}<\/p>\n<p style=\"padding-left: 30px\">dir -Force| where {!$_.psiscontainer}<\/p>\n<p>With no improvements to the <strong>Get-ChildItem<\/strong> cmdlet (<strong>dir<\/strong> is an alias) in Windows PowerShell 3.0, I can simplify the code to find directories to something such as is shown here.<\/p>\n<p style=\"padding-left: 30px\">dir | where psiscontainer<\/p>\n<h2><span style=\"font-size: large\">New switches for the Get-ChildItem cmdlet<\/span><\/h2>\n<p>Improvements to the <strong>Get-ChildItem<\/strong> cmdlet simplify the code required to find files and directories (folders) by permitting direct access via the cmdlet. For example, to find directories using the Get-ChildItem (<em>dir<\/em> is an alias), I use the command shown here.<\/p>\n<p style=\"padding-left: 30px\">dir &ndash;Directory<\/p>\n<p>To find files, I use the command shown here (use the <strong>Force<\/strong> parameter to show hidden and system files.)<\/p>\n<p style=\"padding-left: 30px\">dir -File &ndash;Force<\/p>\n<p>The use of these two commands is shown here.<\/p>\n<p><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\">&nbsp;<\/span><span style=\"font-size: x-small\"> <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1680.HSG-10-9-12-00.png\"><img decoding=\"async\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1680.HSG-10-9-12-00.png\" title=\"Image of command output\" \/><\/a><br \/>&nbsp;<\/span><\/p>\n<p>To find all of the read-only files by using Windows PowerShell 2.0 or Windows PowerShell 1.0 required using either the Match operator or the <strong>Like<\/strong> operator in a <strong>Where-Object<\/strong> command. This type of command is shown here.<\/p>\n<p style=\"padding-left: 30px\">dir -force | where {!$_.psiscontainer -and $_.mode -match &#8216;r&#8217;}<\/p>\n<p>Although this command is not too cryptic for me (but then I have been using and writing about Windows PowerShell since it was called Monad), it takes a lot of time to write such a command&ndash;and for people just coming to Windows PowerShell, a command such as this one is just ridiculous. In Windows PowerShell 3.0 I can shorten the previous command to the one shown here.<\/p>\n<p style=\"padding-left: 30px\">dir -force -File -Attributes readonly<\/p>\n<p>Not only is the command shorter, but it is much easier to read. The command actually makes sense! The two previous commands, and the output associated with each is shown in the figure that follows.<\/p>\n<p><span style=\"font-size: x-small\">&nbsp;<br \/> <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0218.hsg-10-9-12-01.png\"><img decoding=\"async\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0218.hsg-10-9-12-01.png\" title=\"Image of command output\" \/><\/a><\/span><\/p>\n<h2><span style=\"font-size: large\">Using attribute switches<\/span><\/h2>\n<p>To simplify working with the <strong>Get-ChildItem<\/strong> cmdlet, Windows PowerShell 3.0 adds several new attribute switches. The switches are: <strong>ReadOnly<\/strong>, <strong>Hidden<\/strong>, and <strong>System<\/strong>. Therefore, instead of searching for files with the <strong>ReadOnly&nbsp;<\/strong>attribute as I did in the previous command, I can look specifically for read-only<strong>&nbsp;<\/strong>files by using the <strong>ReadOnly&nbsp;<\/strong>switch. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">dir -force -File -ReadOnly<\/p>\n<p>To find only hidden files, the command would change to the one shown here.<\/p>\n<p style=\"padding-left: 30px\">dir -Force -File &ndash;Hidden<\/p>\n<p>Normally, when working with files, I have to specify the <strong>Force <\/strong>parameter to see the hidden and system files. This particular behavior, although it makes sense, is also a terrific potential for error because the <strong>Force <\/strong>parameter is, unfortunately, not implemented in all cmdlets in the same way. Therefore, for some cmdlets you use the <strong>Force<\/strong>&nbsp;parameter, and for other cmdlets you do other things. Luckily, with the new <strong>Hidden <\/strong>and <strong>System<\/strong> switches, this confusion is removed. For example, the following command also lists all the hidden files.<\/p>\n<p style=\"padding-left: 30px\">dir -File &ndash;Hidden<\/p>\n<p>The same behavior does not seem to appear when I list the system files. The reason is that while the <strong>System<\/strong> switch lists system files, all of the system files on my computer are also hidden; and therefore, I will need to use either the Force parameter to show hidden files, or use the <strong>Hidden<\/strong> switch.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;PS C:\\&gt; dir -File -System -Force<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Times New Roman;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Directory: C:\\<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Times New Roman;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Times New Roman;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">-arhs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/14\/2012&nbsp;&nbsp; 6:03 AM&nbsp;&nbsp;&nbsp;&nbsp; 398146 bootmgr<\/p>\n<p style=\"padding-left: 30px\">-a-hs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6\/2\/2012&nbsp; 10:30 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 BOOTNXT<\/p>\n<p style=\"padding-left: 30px\">-a-hs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9\/25\/2012&nbsp;&nbsp; 5:49 PM 6773993472 hiberfil.sys<\/p>\n<p style=\"padding-left: 30px\">-a-hs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9\/25\/2012&nbsp;&nbsp; 5:49 PM 4831838208 pagefile.sys<\/p>\n<p style=\"padding-left: 30px\">-a-hs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9\/25\/2012&nbsp;&nbsp; 5:49 PM&nbsp; 268435456 swapfile.syss seen here.<\/p>\n<p>Join me tomorrow when I will talk about working with file attributes via the <strong>Get-ChildItem<\/strong> cmdlet in Windows PowerShell 3.0.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <span style=\"color: #0000ff\">Facebook<\/span>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> or share you solutions and questions on the <span style=\"color: #0000ff\">Official Scripting Guys Forum<\/span>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use new parameters with the enhanced Get-ChildItem cmdlet in&nbsp;Windows PowerShell 3.0. Microsoft Scripting Guy, Ed Wilson, is here. There are a number of really cool new things in Windows PowerShell 3.0. Some of these make my life a lot easier, and yet they are not the &ldquo;big&rdquo; things. I have written [&hellip;]<\/p>\n","protected":false},"author":597,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,362,3,12,45],"class_list":["post-4851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-powershell-3","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use new parameters with the enhanced Get-ChildItem cmdlet in&nbsp;Windows PowerShell 3.0. Microsoft Scripting Guy, Ed Wilson, is here. There are a number of really cool new things in Windows PowerShell 3.0. Some of these make my life a lot easier, and yet they are not the &ldquo;big&rdquo; things. I have written [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/597"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4851"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4851\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}