Public/Set-SpecFolderHIddenAttribute.ps1
Function Set-SpecFolderHiddenAttribute { <# .SYNOPSIS Sets or removes the hidden attribute of a file or folder. .DESCRIPTION The Set-SpecHiddenAttribute function allows you to add or remove the hidden attribute of a file or folder at the specified path. You can use this function to hide or unhide a file or folder based on the provided action parameter ("Add" or "Remove"). .PARAMETER Path Specifies the path of the file or folder whose hidden attribute will be modified. .PARAMETER Action Specifies whether to "Hide" or "Unhide" the hidden attribute. Use "Hide" to hide the file or folder and "Unhide" to unhide it. .EXAMPLE Set-SpecFolderHiddenAttribute -Path "C:\ExampleFolder" -Action Hide Adds the hidden attribute to the folder located at "C:\ExampleFolder". .EXAMPLE Set-SpecFolderHiddenAttribute -Path "C:\ExampleFolder" -Action Unhide Removes the hidden attribute from the folder located at "C:\ExampleFolder". .EXAMPLE Set-SpecFolderHiddenAttribute -Path "C:\ExampleFolder\myFile.txt" -Action Hide Adds the hidden attribute to the file located at "C:\ExampleFolder\myFile.txt". .EXAMPLE Set-SpecFolderHiddenAttribute -Path "C:\ExampleFolder\myFile.txt" -Action Unhide Removes the hidden attribute from the file located at "C:\ExampleFolder\myFile.txt". .NOTES Author : owen.heaume Changelog - 1.0.0 Initial version - 1.0.1 Change function name for added clarity Change $Action parameter values to 'Hide' and 'Unhide' for clarity #> [cmdletbinding()] param ( [Parameter(Mandatory = $true)] [string]$Path, [Parameter(Mandatory = $true)] [ValidateSet("Hide", "Unhide")] [string]$Action ) try { $fileOrFolder = Get-Item -Path $Path -Force -ea Stop } catch { throw "Unable to get the folder: $path $_" } if ($Action -eq "Hide") { # Check if the file or folder is not hidden, and if so, set the Hidden attribute if (-not ($fileOrFolder.Attributes -band [System.IO.FileAttributes]::Hidden)) { $fileOrFolder.Attributes = $fileOrFolder.Attributes -bor [System.IO.FileAttributes]::Hidden Write-Host "Folder $Path is now hidden." } else { Write-Host "Folder $Path is already hidden." -ForegroundColor darkgray } } elseif ($Action -eq "Unhide") { # Check if the file / folder is hidden, and if so, remove the Hidden attribute if ($fileOrFolder.Attributes -band [System.IO.FileAttributes]::Hidden) { Write-Host "Folder $Path is now not hidden." $fileOrFolder.Attributes = $fileOrFolder.Attributes -bxor [System.IO.FileAttributes]::Hidden } else { Write-Host "Folder $Path is already not hidden." -ForegroundColor darkgray } } } |