{"id":15781,"date":"2011-01-27T00:01:00","date_gmt":"2011-01-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/01\/27\/use-powershell-to-toggle-the-archive-bit-on-files\/"},"modified":"2011-01-27T00:01:00","modified_gmt":"2011-01-27T00:01:00","slug":"use-powershell-to-toggle-the-archive-bit-on-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-toggle-the-archive-bit-on-files\/","title":{"rendered":"Use PowerShell to Toggle the Archive Bit on Files"},"content":{"rendered":"<p>&#160;<\/p>\n<p><b>Summary<\/b>: Learn how to use Windows PowerShell to toggle file attributes such as the archive bit.<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/> Hey, Scripting Guy! I have a number of files that have had the archive bit flipped. Unfortunately, our backup program relies on the archive bit to determine if it has backed up a file recently or if a file has changed and is in need of backup again. What I need is a way to flip all of the archive bits in a folder. <\/p>\n<p>&#8212; BW<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/> Hello BW, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Backup programs. Hmmm, I used to know a person who claimed he had an ideal business model. He said he would advertise extremely low-cost backup tape storage. The backup tapes would be stored in a big hole in the ground. When someone requested a tape from \u201cstorage,\u201d they would be given a blank tape. This model would work, he said, because no one ever tests backup tapes, and no one ever practices a file restore. Luckily, this person never went into business, and never implemented the \u201clow-cost storage\u201d solution. <\/p>\n<p>BW, I have seen the problem you mention occur when someone runs a scheduled daily backup to perform a custom ad hoc backup, instead of creating a custom on-demand job that does not flip archive bits. When this happens, backups get out of sequence, and it can be a real mess. <\/p>\n<blockquote>\n<p>In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/26\/use-a-powershell-cmdlet-to-work-with-file-attributes.aspx\">yesterday\u2019s Hey, Scripting Guy! Blog post<\/a>, I began a discussion of using the <b>Set-ItemProperty<\/b> Windows PowerShell cmdlet to work with archive bits on a file. <\/p>\n<\/blockquote>\n<p>The FSO library on my laptop contains a mixture of files. Some have the archive bit set and others do not. Some are compressed, others encrypted, and still others read-only. The folder is shown in the following image.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/2158.HSG-1-27-11-1_27E3FC78.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of FSO library on Ed&#039;s laptop\" border=\"0\" alt=\"Image of FSO library on Ed&#039;s laptop\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/3247.HSG-1-27-11-1_thumb_4EA1ECEB.jpg\" width=\"604\" height=\"406\" \/><\/a><\/p>\n<p>The Get-FilesWithArchiveBitSet.ps1 script will go through a folder and report files that have the archive bit set. It does not matter if there are additional file attributes; the script will detect the presence of the archive bit. The use of the <b>io.fileattributes<\/b> enumeration was discussed in yesterday\u2019s Hey, Scripting Guy! Blog post. <\/p>\n<p>The key to the script is the use of the <b>If<\/b> statement to perform a bitwise AND operation on the file attributes:<\/p>\n<blockquote>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\" class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">If((Get-ItemProperty -Path $file.fullname).attributes -band $attribute)<\/font><\/font><\/span><\/p>\n<\/blockquote>\n<p>If the attribute is present, it is reported in green. If the attribute is not present, it is reported in blue:<\/p>\n<blockquote>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\" class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">{ Write-Host -ForegroundColor green `<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\" class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;$file.fullname has the $attribute bit set&quot; }<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\" class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;<\/font><\/span><font style=\"font-size: 10pt\">ELSE<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\" class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160; <\/font><\/span><font style=\"font-size: 10pt\">{ Write-host -ForegroundColor blue `<\/font><\/font><\/font><\/span><\/p>\n<p>   <span><font face=\"Segoe\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;$file.fullname does not have the $attribute bit set&quot;}<\/font><\/font><\/font><\/span><\/p><\/blockquote>\n<p>The complete script is shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 11pt;list-style-type: disc;margin: 15pt 12pt 0pt 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreenedHead\"><span><font face=\"Segoe Semibold\"><font style=\"font-size: 10pt\" color=\"#000000\">Get-FilesWithArchiveBitSet.ps1<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">$path = &quot;C:\\fso&quot;<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">$files = Get-ChildItem -Path $path -Recurse<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">$attribute = [io.fileattributes]::archive<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">&#160;<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">Foreach($file in $files)<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">{<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;<\/font><\/span><font style=\"font-size: 10pt\">If((Get-ItemProperty -Path $file.fullname).attributes -band $attribute)<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160; <\/font><\/span><font style=\"font-size: 10pt\">{ Write-Host -ForegroundColor green `<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;$file.fullname has the $attribute bit set&quot; }<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;<\/font><\/span><font style=\"font-size: 10pt\">ELSE<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160; <\/font><\/span><font style=\"font-size: 10pt\">{ Write-host -ForegroundColor blue `<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;$file.fullname does not have the $attribute bit set&quot;}<\/font><\/font><\/font><\/span><\/p>\n<p>   <span><font face=\"Segoe\"><font style=\"font-size: 10pt\" color=\"#000000\">} #end Foreach<\/font><\/font><\/span><\/p><\/blockquote>\n<p>When the Get-FilesWithArchiveBitSet.ps1 script runs, the output appears that is shown in the following image:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8015.HSG-1-27-11-2_1DBF3580.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of script output\" border=\"0\" alt=\"Image of script output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/7457.HSG-1-27-11-2_thumb_6EACD3DB.jpg\" width=\"604\" height=\"472\" \/><\/a><\/p>\n<p>Now that we understand the basic process, let\u2019s add one additional concept. To toggle the archive bit, I need to use a Bitwise Exclusive Or (BXOR) operator. <\/p>\n<p>In the table that appears here, the first row is the decimal value of the position. In the second row is the binary representation of the number 33 that I obtained by using the <b>convert<\/b> class. In the second row, notice there is a 1 in the 32 column, and a 1 in the 1 column. The third row is the binary value of the number 1 (the value of the read-only enumeration). The fourth row represents the results of performing a bitwise Exclusive OR (BXOR) operation. When performing a BXOR operation, the rules are thus: <\/p>\n<p>1 \u2013BXOR 1 = 0, 1 \u2013BXOR 0 = 1, 0 \u2013BXOR 0 = 0. <\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"74\">\n<p>128<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>64<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>32<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>16<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\" width=\"74\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\" width=\"73\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I can use this information to determine the attributes other than the archive bit of a file. This is illustrated here. Notice that when I use <b>\u2013bxor<\/b> on the file attributes, and <b>\u2013bxor<\/b> with the archive bit, what remains is the <b>ReadOnly<\/b> attribute. <\/p>\n<blockquote>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">PS C:\\&gt; (Get-ItemProperty C:\\fso\\a.txt).attributes<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">ReadOnly, Archive<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">PS C:\\&gt; (Get-ItemProperty C:\\fso\\a.txt).attributes -bxor [io.fileattributes]::Archive<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">1<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">PS C:\\&gt;<\/font><\/font><\/span><span><\/span><\/p>\n<\/blockquote>\n<p>Armed with this information, I create a script that will toggle the archive bit of a file. If the archive bit exists, it is removed; if it does not exist, it is added to the file. The Set-FilesWithArchiveBit.ps1 script is shown here.<\/p>\n<blockquote>\n<p style=\"line-height: 11pt;list-style-type: disc;margin: 15pt 12pt 0pt 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreenedHead\"><span><font face=\"Segoe Semibold\"><font style=\"font-size: 10pt\" color=\"#000000\">Set-FilesWithArchiveBit.ps1<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">$path = &quot;C:\\fso&quot;<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">$files = Get-ChildItem -Path $path -Recurse<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">$attribute = [io.fileattributes]::archive<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">&#160;<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">Foreach($file in $files)<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">{<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;<\/font><\/span><font style=\"font-size: 10pt\">If((Get-ItemProperty -Path $file.fullname).attributes -band $attribute)<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160; <\/font><\/span><font style=\"font-size: 10pt\">{ <\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;$file.fullname has the $attribute bit set, removing the bit.&quot;<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">Set-ItemProperty -Path $file.fullname -Name attributes `<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">-Value ((Get-ItemProperty $file.fullname).attributes -BXOR $attribute)<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;New value of $file.Fullname attributes&quot;<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">(Get-ItemProperty -Path $file.fullname).attributes<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160; <\/font><\/span><font style=\"font-size: 10pt\">}<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;<\/font><\/span><font style=\"font-size: 10pt\">ELSE<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160; <\/font><\/span><font style=\"font-size: 10pt\">{ <\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">Write-host -ForegroundColor blue `<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;$file.fullname does not have the $attribute bit set, setting the bit.&quot;<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">Set-ItemProperty -Path $file.fullname -Name attributes `<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">-Value ((Get-ItemProperty $file.fullname).attributes -BXOR $attribute)<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">&quot;New value of $file.Fullname attributes&quot;<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">(Get-ItemProperty -Path $file.fullname).attributes<\/font><\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: none transparent scroll repeat 0% 0%\" class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">}<\/font><\/font><\/font><\/span><\/p>\n<p>   <span><font face=\"Segoe\"><font style=\"font-size: 10pt\" color=\"#000000\">} #end Foreach<\/font><\/font><\/span><\/p><\/blockquote>\n<p>The Set-FilesWithArchiveBit.ps1 script is similar to the earlier script except that the <b>Set-ItemProperty<\/b> Windows PowerShell cmdlet is used to change the value of the attributes of the file. To do this, I use <b>Get-ItemProperty<\/b> to retrieve the file attributes, and then I use BXOR to perform a bitwise exclusive OR operation with my chosen attribute. This portion of the code is shown here:<\/p>\n<blockquote>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\" class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><font style=\"font-size: 10pt\" color=\"#000000\">Set-ItemProperty -Path $file.fullname -Name attributes `<\/font><\/font><\/span><\/p>\n<p style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\" class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><font color=\"#000000\"><span><font style=\"font-size: 10pt\">&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 10pt\">-Value ((Get-ItemProperty $file.fullname).attributes -BXOR $attribute)<\/font><\/font><\/font><\/span><\/p>\n<\/blockquote>\n<p>When the Set-FilesWithArchiveBit.ps1 script runs, the output appears that is shown in the following image:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/2642.HSG-1-27-11-3_3E364F65.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of script output\" border=\"0\" alt=\"Image of script output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8446.HSG-1-27-11-3_thumb_4D89B174.jpg\" width=\"604\" height=\"516\" \/><\/a><\/p>\n<p>One thing to keep in mind about using the <b>Set-ItemProperty<\/b> cmdlet to work with file attributes through the <b>FileSystem<\/b> provider is that it is limited to working with the following attributes: <b>Archive<\/b>, <b>Hidden<\/b>, <b>Normal<\/b>, <b>ReadOnly<\/b>, or <b>System<\/b>. An error will be generated, and the attributes will not be modified if a file is compressed or encrypted.<\/p>\n<p>BW, that is all there is to using the <b>Set-ItemProperty<\/b> cmdlet to toggle the archive bit. <\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&#160;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#160; Summary: Learn how to use Windows PowerShell to toggle file attributes such as the archive bit. Hey, Scripting Guy! I have a number of files that have had the archive bit flipped. Unfortunately, our backup program relies on the archive bit to determine if it has backed up a file recently or if a [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,51,3,4,12,45],"class_list":["post-15781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&#160; Summary: Learn how to use Windows PowerShell to toggle file attributes such as the archive bit. Hey, Scripting Guy! I have a number of files that have had the archive bit flipped. Unfortunately, our backup program relies on the archive bit to determine if it has backed up a file recently or if a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15781","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15781"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15781\/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=15781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}