Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Get-ChildItem not to follow links

Tags:

powershell

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.

like image 673
INDIVIDUAL-IT Avatar asked Apr 21 '15 05:04

INDIVIDUAL-IT


People also ask

How do I Get-ChildItem to exclude folders?

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.

What does Get-ChildItem do?

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.

How do you get the full path of ChildItem?

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.

What is GCI in PowerShell?

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.


1 Answers

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
like image 133
David Brabant Avatar answered Sep 21 '22 17:09

David Brabant