New
#1
Scripot to create folders
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 on each drive. So for example, assuming I have drives C, D, E, F, it would search E and F only and find all folders/sub folders on E and F and then create a script that I can use to create those folders/sub-folders on my new server. 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 so much.
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"