Scripot to create folders


  1. Posts : 8
    Windows 10 Professional
       #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"
      My Computer


  2. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    Or use the built-in utility RoboCopy to mirror the drives from one computer to the other.
    Sample RoboCopy backing up script [post #36] - TenForums
    You can see its built-in Help by entering this in a command window RoboCopy /?
    Robocopy - SS64
    RoboCopy - MSDocs
    This is still relevant ROBOCOPY - Create Backup Script - SevenForumsTutorials


    Best of luck,
    Denis
      My Computer


  3. Posts : 18,034
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #3

    Hello @shahgols,

    Is this of any use ? > Fast and safe way to copy data partition onto new disk?

    I hope this helps.
      My Computer


  4. Posts : 8
    Windows 10 Professional
    Thread Starter
       #4

    Thank you both for the quick response, I'll look into it. Have a great weekend!
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 07:02.
Find Us




Windows 10 Forums