How to loop through all directories in a drive using powershell?

Member

by mac , in category: Third Party Scripts , 2 months ago

How to loop through all directories in a drive using powershell?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 2 months ago

@mac 

You can loop through all directories in a drive using PowerShell by using the Get-ChildItem cmdlet along with the -Recurse parameter. Here is an example script that loops through all directories in the C: drive:

1
2
3
Get-ChildItem -Path C: -Directory -Recurse | ForEach-Object {
    Write-Host $_.FullName
}


In this script:

  • Get-ChildItem -Path C: -Directory -Recurse retrieves all directories in the C: drive recursively.
  • ForEach-Object loops through each directory.
  • Write-Host $_.FullName displays the full path of each directory.


You can replace C: with the drive letter you want to loop through.