Public/Set-SpecHIddenAttribute.ps1
Function Set-SpecHiddenAttribute { <# .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 "Add" or "Remove" the hidden attribute. Use "Add" to hide the file or folder and "Remove" to unhide it. .OUTPUTS Returns 0 if the operation is successful. Returns 1 if the folder is already in the desired state (hidden or not hidden). .EXAMPLE Set-SpecHiddenAttribute -Path "C:\ExampleFolder" -Action Add Adds the hidden attribute to the folder located at "C:\ExampleFolder". .EXAMPLE Set-SpecHiddenAttribute -Path "C:\ExampleFolder" -Action Remove Removes the hidden attribute from the folder located at "C:\ExampleFolder". .EXAMPLE Set-SpecHiddenAttribute -Path "C:\ExampleFolder\myFile.txt" -Action Add Adds the hidden attribute to the file located at "C:\ExampleFolder\myFile.txt". .EXAMPLE Set-SpecHiddenAttribute -Path "C:\ExampleFolder\myFile.txt" -Action Remove Removes the hidden attribute from the file located at "C:\ExampleFolder\myFile.txt". .NOTES Author : owen.heaume Version : 1.0 #> [cmdletbinding()] param ( [Parameter(Mandatory = $true)] [string]$Path, [Parameter(Mandatory = $true)] [ValidateSet("Add", "Remove")] [string]$Action ) $fileOrFolder = Get-Item -Path $Path -Force if ($Action -eq "Add") { # 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-Verbose "Hidden attribute added to folder $Path." return 0 } else { Write-Verbose "Folder $Path is already hidden." return 1 } } elseif ($Action -eq "Remove") { # Check if the file / folder is hidden, and if so, remove the Hidden attribute if ($fileOrFolder.Attributes -band [System.IO.FileAttributes]::Hidden) { $fileOrFolder.Attributes = $fileOrFolder.Attributes -bxor [System.IO.FileAttributes]::Hidden Write-Verbose "Hidden attribute removed from folder $Path." return 0 } else { Write-Verbose "Folder $Path is not hidden." return 1 } } } |