I need Get-ChildItem
to return all files/folders inside a path. But there are also symlinks going of to remote servers. The links are created by: mklink /D link \\remote-server\folder
If I run Get-ChildItem -recurse
it follows the links to the remote servers and lists all files/folders from there. If I use -exclude it does not list the folder matching the excluded pattern but still goes down and lists all included files and folders.
What I actually need is Get-ChildItem -recurse
to ignore links at all and not to follow them.
To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.
Use the Get-ChildItem cmdlet in PowerShell to get the files in the folder using the file filter and using the Select-Object cmdlet to get the full path of the file using ExpandProperty FullName.
Get-ChildItem (GCI) gets items and if the item is a container, it will get child items available inside the container. Location specified in PowerShell Get-ChildItem can be file system directory, registry, or certificate store. Let's understand the PowerShell Get-ChildItem cmdlet with examples.
You can exclude links using a command like below:
gci <your path> -Force -Recurse -ErrorAction 'silentlycontinue' |
Where { !($_.Attributes -match "ReparsePoint") }
After reading your comment: can't you do it in two steps?
$excluded = @(gci <your path> -Force -Recurse -ErrorAction 'silentlycontine' | where { ($_.Attributes -match "ReparsePoint") )
get-childitem -path <your path> -recurse -exclude $excluded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With