powershellsystem.io.filesystem.io.directory

How to hidde a directory or a folder with the Set-itemProperty command?


$Path|Set-ItemProperty -name Hidden -Value $true
$Path|Set-ItemProperty -name isHidden -Value $true
Set-ItemProperty -path $Path -name isHidden -Value $true

#Error: Set-ItemProperty: The property bool isHidden=False does not exist or was not found.

There is no argument completion for -name property, so its hard to tell what it supports or not. I know how to lock a file with it, only because it is one of the examples in the docs:

$Path|Set-ItemProperty -name IsReadOnly -Value $true

Any help would be greatly appreciated!


Solution

  • Santiago's helpful answer provides an effective solution that relies on direct use of .NET type members.

    An even simpler solution is to use the standard attrib.exe utility, whose sole purpose is to manage file attributes:

    # Turn on the Hidden attribute; -h would turn it off.
    attrib +h $Path
    

    As for using Set-ItemProperty:

    On Windows,[1] it is the Attributes property that you need to target, and while
    Set-ItemProperty -Path $Path -Name Attributes -Value Hidden does work in principle, the caveat is:


    [1] On Unix-like platforms, visibility is determined simply by an item's (file or directory's) name: if the name starts with ., the item is considered to be hidden. macOS, specifically, supports an additional mechanism for hiding items, via extended file-system attributes.

    [2] The Directory attribute is special, however: it is invariably set for directories and cannot be cleared.