none
Making files hidden with powershell RRS feed

  • Question

  • Hi Scripting forum!

    I have read several different forum posts and articles (including the one's put out by the scripting guys in 2011) about how to use set-itemproperty to make a file hidden using this command:

    $file = ".\file.txt"
    
    Set-ItemProperty -path $file -name  [io.fileattributes]::Hidden)

    However, when I run that, it asks for a value.  Logic would dictate that you respond with $true and it would make the file hidden...but when I respond that way, I get an error saying that hidden is a read only attribute.  So, how do I remove the read only status on the hidden attribute, and after that, make the file hidden.

    The end goal is for the script to see the file when get-childitem -property hidden is run, unhide the file using set-itemproperty, run the existing part of the script (check last modified date, copy and replace to desktop if newer exists), and use set-itemproperty again to rehide the file...then open the file. (Painful, right?)

    Hopefully, at this point you're asking "Obu, why on Kim Stanley Robinson's Green Mars do you need to do this horribly complex thing?  Short answer:  One user (of several) that uses the script on the computer in question doesn't like icons on the desktop because they confuse him. -_-

    Anyways, any and all help will be appreciated!


    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Saturday, December 8, 2018 7:08 AM

Answers

  • To add the hidden attribute to a file just do this.

    (get-item test.txt).Attributes += 'Hidden'

    To remove the attribute:

    (get-item test.txt -force).Attributes -= 'Hidden'

    These methods preserve existing attributes.


    \_(ツ)_/

    • Marked as answer by Obujuwami Thursday, December 13, 2018 6:05 PM
    Saturday, December 8, 2018 8:05 AM
  • You should test for the attribute first to avoid exceptions:

    (get-item test.txt -Force).Attributes.HasFlag([System.IO.FileAttributes]::Hidden)

    This method requires the enum to succeed.

    You can also do this:

    (get-item test.txt -force).Attributes -match 'Hidden'


    \_(ツ)_/

    • Marked as answer by Obujuwami Thursday, December 13, 2018 6:05 PM
    Saturday, December 8, 2018 8:13 AM

All replies

  • you can try to change attributes by Get-Item and then change the attribute you want. for example:

    $MyFile=get-item .\your_folder_or_File -Force
    $MyFile.attributes="Hidden"

    this code will change the hidden attribute of a file. to hidden everything within a folder try this:

    Get-ChildItem -path "your_file_path" -Recurse -Force | foreach {$_.attributes = "Hidden"} 
    Saturday, December 8, 2018 7:35 AM
  • To add the hidden attribute to a file just do this.

    (get-item test.txt).Attributes += 'Hidden'

    To remove the attribute:

    (get-item test.txt -force).Attributes -= 'Hidden'

    These methods preserve existing attributes.


    \_(ツ)_/

    • Marked as answer by Obujuwami Thursday, December 13, 2018 6:05 PM
    Saturday, December 8, 2018 8:05 AM
  • You should test for the attribute first to avoid exceptions:

    (get-item test.txt -Force).Attributes.HasFlag([System.IO.FileAttributes]::Hidden)

    This method requires the enum to succeed.

    You can also do this:

    (get-item test.txt -force).Attributes -match 'Hidden'


    \_(ツ)_/

    • Marked as answer by Obujuwami Thursday, December 13, 2018 6:05 PM
    Saturday, December 8, 2018 8:13 AM
  • Hey JRV,

    Thanks for the response!  It looks like that did the trick, and I included your testing code before hand.  I plan to use it in a if/then/else statement so that I don't get errors when hiding the file in the event it gets updated.

    Thanks!


    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Thursday, December 13, 2018 6:05 PM