Hi all, I asked chatgpt to create a powershell script for me that will search all drives (other than the C and D) drives for all folders and sub-folders and generate a script to create those folders/sub-folders. It came up with the below script, but it is not working and since I am not familiar with powershell, I am unable to fix it. Can someone please look into this and find out why it is not working? I would highly, highly appreciate it, it will save me a lot of work! Thank you in advance!
Code:
$excludedDrives = @("C", "D")
$scriptOutputPath = "C:\Scripts\CreateDirectories.ps1"
$drives = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 -and $_.DeviceID -notin $excludedDrives }
$scriptContent = @()
foreach ($drive in $drives) {
$rootPath = $drive.DeviceID + "\"
$directories = Get-ChildItem -Path $rootPath -Directory -Recurse | Select-Object -ExpandProperty FullName
foreach ($directory in $directories) {
$relativePath = $directory.Substring($rootPath.Length)
$scriptContent += "New-Item -Path `"C:\$relativePath`" -ItemType Directory -Force"
}
}
$scriptContent | Out-File -FilePath $scriptOutputPath
Write-Host "Script generated successfully at: $scriptOutputPath"
My Computer
- Computer Manufacturer/Model Number
- ASUS G73J laptop
- OS
- 7 Home Premium 64-bit