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

Member

by mac , in category: Third Party Scripts , a month ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , a month 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.