2

For any location gotten by the Get-Location cmdlet, if I have a path like this:

C:\Top\Middle\Bottom

I would say it's 3 levels deep.

If I have:

C:\

I would say it's the 0 level. It could be 0 or 1 but the logic of depth is what matters.

Is there a way to figure this out?

2 Answers 2

1

I think the simplest way of doing this (assuming that your files do not have a \ in their names) is like:

$file.FullName.ToString().Split('\\').Count

2
  • In my case it'd be (Get-Location).Path.ToString().Split('\\').Count right? It gives 2 for both C:\ and C:\Top. For all deeper levels it works fine. Jan 13, 2015 at 4:04
  • @laggingreflex I think thats correct. If you want you can special case drive letters only if you want (length will be three, and those are always drive letters)
    – soandos
    Jan 13, 2015 at 4:14
-1

This code:

((Get-Location).Path.ToString().Split('\\') | ? {$_ -ne ""} ).Count

Returns 1 for c:\

Returns 2 for c:\users\

1
  • (Get-Location).Path.TrimEnd('\').split('\').count May 21, 2019 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .