Any powershell gurus out there?

shahgols

New member
Local time
4:08 PM
Messages
11
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
I don't know Powershell, but there are some that do at Windows 10 Help Forums

Maybe it was how you asked the question. I'm sure different ways of asking the question will yield different results. I actually don't understand the question myself. You want all non C and D rives to have subfolders created in them? Or do you simply want to search inside the non C and D drives and create a list of those non C and D drives' folders?

I use Everything.exe and you could probably do this with that program. Not create a list, but use regex and search parameters to access non C and D drives and their folders.
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 Ultimate x64
Thank you F22, I'll post on that forum. I'm sorry for the vague request, I'll try to make it better. Thanks again!
 

My Computer

Computer Manufacturer/Model Number
ASUS G73J laptop
OS
7 Home Premium 64-bit
This looks very likely to be a XY problem, so I'll start to ask, what's your real problem you want to solve? What do you want to do with such a script?

The next thing is two serious mistakes. One is using ChatGPT for anything programming related. It's widely known that it tends to produce poor qualiy or outright wrong solutions for questions, in addition to the usual AI hallucinations. There is a reason why Stack Overflow (the most famous programming forum out there) completely banned its use.
Another important thing is that you're looking for a script in a language you don't understand, therefore you can never evaluate what it's actually doing or make any modifications you should need them. If search for a pre-made solution, look for a language you already know, or at least, are willing to learn in the process.

Most likely, if the actual problem is simple or common enough, a ready-made script or a whole program already exists and you can use that instead of developing your own (F22 Simpilot already mentioned Everything, for example), unless you're doing it for fun or learning.

I won't go for now into reviewing the actual script posted, as I don't understand the requirement behind it, so any corrections could be completely wrong :D
Also, what does it means "not working"? Please explain what you're seeing and what you expected.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Sattelite A665-S6092
OS
Windows 7 Ultimate x64
CPU
Intel Core i7-740QM
Memory
8 GB DDR3
Graphics Card(s)
NVIDIA GeForce 330GT
Screen Resolution
1366x768
Hard Drives
Samsung 840 SSD 500GB
1TB USB3 external HD
Cooling
Coolermaster Notepal U3 notebook cooling pad
Internet Speed
3mbps ASDL
Antivirus
ClamWin 0.98.7
Browser
Opera 12.17 x86 (main), Firefox 38 (sec), IE11 (last resort)
Back
Top