locked
How to Tweak this PS Script to Add Additional Folders RRS feed

  • Question

  • I run the following to create user folders on multiple servers, but it only creates 1 folder at a time.  I'm going to need to create hundreds soon, so would like to tweak it to create multiple user folders in one shot.

    $Servers='server0','server1','server2'
    $NewFolder = 'user0'
    $Servers | foreach { New-Item "\\$($_)\c$\users\$NewFolder" -ItemType Dir }

    I tried the obvious:

    $Servers='server0','server1','server2'
    $NewFolder = 'test0','test1','test2'
    $Servers | foreach { New-Item "\\$($_)\c$\users\$NewFolder" -ItemType Dir }

    But that created 1 folder named "test0 test1 test2". :)

    Any help is appreciated!

    Friday, December 30, 2016 5:27 PM

Answers

  • As you've noted, you can use two arrays (one containing a list of computer names, and the other containing the list of new folder names). Then just use two foreach loops. For example (not tested):


    foreach ( $server in $servers ) {
      foreach ( $folder in $folders ) {
        New-Item "\\$server\C$\Users\$folder" -ItemType Directory -WhatIf
      }
    }
    

    Of course, remove the -WhatIf parameter to actually create the folders.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Darren Blanchard Friday, December 30, 2016 7:29 PM
    • Marked as answer by dgower2 Friday, December 30, 2016 9:44 PM
    Friday, December 30, 2016 5:57 PM

All replies

  • As you've noted, you can use two arrays (one containing a list of computer names, and the other containing the list of new folder names). Then just use two foreach loops. For example (not tested):


    foreach ( $server in $servers ) {
      foreach ( $folder in $folders ) {
        New-Item "\\$server\C$\Users\$folder" -ItemType Directory -WhatIf
      }
    }
    

    Of course, remove the -WhatIf parameter to actually create the folders.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Darren Blanchard Friday, December 30, 2016 7:29 PM
    • Marked as answer by dgower2 Friday, December 30, 2016 9:44 PM
    Friday, December 30, 2016 5:57 PM
  • As an aside: If this is a standard part of your user configuration, I would recommend creating needed folders using Group Policy Preferences rather than using a script.

    -- Bill Stewart [Bill_Stewart]

    Friday, December 30, 2016 6:00 PM
  • It's only for users of a specific application and only on 7 servers that are used for that application.  Do you still think Group Policy would make more sense?  I'm also not a domain admin, so I would have to get another group to create the GP.  The PS script I can run myself.  Thanks for the reply.  I'll start playing with the foreach loop.
    Friday, December 30, 2016 7:26 PM
  • That depends on your organization. But GPOs are not within the scope of this forum. (There's a Group Policy forum for GPO questions.)


    -- Bill Stewart [Bill_Stewart]

    Friday, December 30, 2016 7:47 PM
  • I'd rather just do it myself anyway.  It's easier.  So, I thought I could figure out the rest with your example above, but I really have no idea how to do it.

    $Servers='server1'
    $NewFolder = 'test0','test1','test2'
    foreach ( $server in $servers ) {
      foreach ( $NewFolder in $NewFolders ) {
        New-Item "\\$server\C$\Users\$folder" -ItemType Directory
      }
    }

    This didn't work.  Sorry, I don't have a programming background.

    TIA

    Friday, December 30, 2016 8:33 PM
  • Look at your New-Item line - you are trying to create $folder rather than $NewFolder. (You changed the variable name in the inner foreach loop from what I posted.)

    -- Bill Stewart [Bill_Stewart]


    Friday, December 30, 2016 8:36 PM
  • $Servers= 'server1'
    $Folders = 'test0','test1','test2'
    foreach ( $server in $servers ) {
      foreach ( $Folder in $Folders ) {
        New-Item "\\$server\C$\users\$folder" -ItemType Directory
      }
    }

    This worked!  Thanks!

    Friday, December 30, 2016 9:44 PM