Home » How to Get Hidden Files Using Get-ChildItem in PowerShell

How to Get Hidden Files Using Get-ChildItem in PowerShell

To get hidden files using the Get-ChildItem cmdlet in PowerShell, you can use the -Force parameter or the -Hidden switch parameter.

The following methods show how you can do it with syntax.

Method 1: Using the -Force parameter

Get-ChildItem -Force

This example will return hidden files and folders in the current directory.

Method 2: Using the -Hidden parameter

Get-ChildItem -Hidden

This example will return hidden files and folders in the current directory.

The following examples show how you can use these methods.

How to Get Hidden Files Using Get-ChildItem with -Force Parameter

You can retrieve hidden files and folders using the Get-ChildItem cmdlet with the -Force parameter. It retrieves all items in the specified directory including hidden files and folders.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# specify the directory path
$folderPath = "C:\temp\log"
# Get hidden files in a specific directory
$files = Get-ChildItem -Path $folderPath -Force
Write-Output $files
# specify the directory path $folderPath = "C:\temp\log" # Get hidden files in a specific directory $files = Get-ChildItem -Path $folderPath -Force Write-Output $files
# specify the directory path
$folderPath = "C:\temp\log"

# Get hidden files in a specific directory
$files = Get-ChildItem -Path $folderPath -Force

Write-Output $files

Output:

PowerShell get hidden files using get-childitem -force parameter
PowerShell get hidden files using Get-ChildItem -force parameter

In this script, we define a $folder variable that stores the path to the directory from where we want to retrieve hidden files or folders.

We then use the Get-ChildItem cmdlet in PowerShell with the -Force parameter to retrieve all files and folders including hidden files in the specified $folderPath and store them in the $files variable.

Finally, we use the Write-Output cmdlet to output the hidden files or folders. In the output screenshot, the hidden files are shown by Mode -h.

How to Get Hidden Files or Folders Using the Get-ChildItem with -Hidden Parameter

Another option to get hidden files and folders is to use the Get-ChildItem cmdlet in PowerShell with the -Hidden parameter.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# specify the directory path
$folderPath = "C:\temp\log"
# Get hidden files in a specific directory
$files = Get-ChildItem -Path $folderPath -Hidden
Write-Output $files
# specify the directory path $folderPath = "C:\temp\log" # Get hidden files in a specific directory $files = Get-ChildItem -Path $folderPath -Hidden Write-Output $files
# specify the directory path
$folderPath = "C:\temp\log"

# Get hidden files in a specific directory
$files = Get-ChildItem -Path $folderPath -Hidden

Write-Output $files

Output:

PowerShell get hidden files using get-childitem -hidden parameter
PowerShell get hidden files using Get-Childitem -Hidden parameter

In this script, we define a variable $folderPath to store the directory path.

We then use the Get-ChildItem cmdlet in PowerShell with the -Hidden parameter to retrieve hidden files and folders in a specific directory and store them in the $files variable.

Finally, we use the Write-Output cmdlet to output the hidden files and folders.

You can also get hidden files recursively in the specified directory and its subdirectories by using the following syntax.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# specify the directory path
$folderPath = "C:\temp\log"
# Get hidden files in a specific directory
$files = Get-ChildItem -Path $folderPath -Force -Recurse
Write-Output $files
# specify the directory path $folderPath = "C:\temp\log" # Get hidden files in a specific directory $files = Get-ChildItem -Path $folderPath -Force -Recurse Write-Output $files
# specify the directory path
$folderPath = "C:\temp\log"

# Get hidden files in a specific directory
$files = Get-ChildItem -Path $folderPath -Force -Recurse

Write-Output $files

Output:

(base) PS C:\> $files = Get-ChildItem -Path $folderPath -Hidden -Recurse
(base) PS C:\> Write-Output $files


    Directory: C:\temp\log


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a-h--         4/16/2024   3:49 PM              2 syslog.txt
-a-h--         4/16/2024  11:12 AM            147 win_log.log


    Directory: C:\temp\log\System log


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a-h--         4/13/2024   8:12 AM            120 security_log.txt


(base) PS C:\>




It retrieves hidden files from the directory and subdirectories.

Conclusion

I hope the above article on getting hidden files and folders in the Windows system using the Get-ChildItem cmdlet in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.