简体   繁体   中英

PowerShell one line command to display names of local git repositories

I'm trying to write a simple command that will show me where all the git repositories are on my local disk. The following command

get-childitem -path d:\ .git -recurse -directory -hidden

displays all the desired information, but also displays the following error message:

get-childitem : Access to the path 'D:\System Volume Information' is denied.
At line:1 char:1
+ get-childitem -path d:\ .git -recurse -directory -hidden
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (D:\System Volume Information:String) [Get
   -ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.Get
   ChildItemCommand

I've tried a variety of -filter and -exclude options to prevent the command from trying to access the forbidden directory, but none have had the desired effect.

Without explicitly calling it, there is not a way to tell powershell "don't call a directory you can't reach". Instead, you have to let powershell try to access it, then handle the error when it occurs.

I may be wrong, but it sounds like you are concerned with the error interrupting your flow of data. It does not sound like you actually care if powershell is trying to access the hidden directory. If that is the case, I recommend suppressing the errors and storing in a variable. Then, if you want to see what failed, you can call the variable. For example:

get-childitem -path d:\ .git -recurse -directory -hidden -ErrorAction SilentlyContinue -ErrorVariable bad

# Display the errors

$bad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM