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

70 Views Asked by At
$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!

2

There are 2 best solutions below

0
mklement0 On BEST ANSWER

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:

  • The -Value argument invariably fully replaces the existing attributes, so that you'd need to make sure to also include any preexisting attributes in order to preserve them if your intent is to selectively set attributes:[2]

    • E.g., if the target item has the Archive attribute set, and you needed to preserve that, you'd need to specify -Value 'Hidden, Archive'.

      • That is, you'd need to specify a single string containing a ,-separated list of the symbolic names of System.IO.FileAttributes enumeration values, relying on PowerShell's convenient to-and-from-string conversion of System.Enum-derived types.
        (The cumbersome type-exact equivalent of the above would be:
        -Value ([System.IO.FileAttributes]::Hidden -bor [System.IO.FileAttributes]::Archive))

      • Thus, in order to clear all attributes (that can be cleared), pass -Value 'None'

    • Operating on the .Attributes property of System.IO.FileSystemInfo instances, as output by Get-Item or Get-ChildItem, makes basing the updated value on the existing attributes easier, as shown in Santiago's answer.


[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.

0
Santiago Squarzon On

You don't really need to use Set-ItemProperty, hiding a file or folder can be done by adding the FileAttributes.Hidden flag to the .Attributes property of a FileSystemInfo instance (base class of FileInfo and DirectoryInfo):

$fileOrFolder = Get-Item path\to\fileORfolder
$fileOrFolder.Attributes = $fileOrFolder.Attributes -bor [System.IO.FileAttributes]::Hidden

Same applies for .IsReadOnly you can set it to $true or $false at ease:

$fileOrFolder.IsReadOnly = $true

If you want to get a better understanding on what you can set on a file or folder I recommend you to go to the .NET docs, look for the Properties section and then look for settable properties (where it says "Gets or sets...").