none
Need Help Creating Child Folders for New Users. RRS feed

  • Question

  • Hello Scripting Guys,

    I'm completely new to scripting and reach out to the experts for assistance.  I have been tasked to come up with a Powershell script that will query Active Directory for any new user that has been created (we use an application to create new users that sets a homedirectory as \\server\%username%$ with the actual path of \\server\h\users\%username%) within the last 5 minutes and create 2 child directories under the users root share.  i.e. \\server\%username%$\folder1 and \\server\%username%$\folder2.  The application we use to create user accounts also has the ability to attach a script to run when the user account is created.  I'm trying to create a script and attach it to the application.


    After scouring the net for a week I have come up with something that looks as though it may work (from my extremely limited experience) but, nonetheless, does not.

    $fiveminutes = (Get-Date).AddMinutes(-5)
    
    $newuser = Get-ADUser -Filter * -Properties * | where { $_.whenCreated -ge $fiveminutes } | select homeDirectory
    
    $folders = gci $newuser | Where-Object{($_.PSIsContainer)} | foreach-object{$_.Name}
    
    ForEach($fldr in $folders){ New-Item -ItemType Directory -Path "\\$newuser\$fldr\Folder1"}
    ForEach($fldr in $folders){ New-Item -ItemType Directory -Path "\\$newuser\$fldr\Folder2"}

    Basically, I'm trying to query A.D. for any new user that was created in the last 5 minutes, grab the homeDirectory location and create the 2 new folders within it.  As you can probably tell, this isn't working out too well.  When I break it down it does complete the search and pull new users and display their homeDirectory but Powershell errors out in line 7 when trying to create the folders.

    New-Item : The path is not of a legal form.
    At C:\QueryADCreateFolders.ps1:7 char:37
    + ForEach($fldr in $folders){ New-Item <<<<  -ItemType Directory -Path "\\$newu
    ser\$fldr\Folder1"}
        + CategoryInfo          : InvalidArgument: (\\\Intel\Folder1:String) [New-
       Item], ArgumentException
        + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShel
       l.Commands.NewItemCommand
    

    I've come across many forums where people would recommend Group Policy to create these folders.  Unfortunately, we need these child folders created before the user logs in. (Due to another application that requires these folders be created, which checks for them upon login) Oh and the permissions need to be inhereted from the parent folder (\\server\%username%$).

    I'm open to any suggestions.  Thank you!

    Wednesday, December 4, 2013 4:01 PM

Answers

  • Hello Dayrowshay,

    welcome to the world of scripting!

    First of all, a minor edit to your script that ought to make it work:

    # Set timespan
    $fiveminutes = (Get-Date).AddMinutes(-5)
    
    # Grab users.
    $newusers = Get-ADUser -Filter {whenCreated -ge $fiveminutes} -Properties HomeDirectory
    
    # For each user ...
    foreach ($user in $newuser)
    {
    	# Create the two folders
    	New-Item -ItemType Directory -Path $user.HomeDirectory -Name "Folder1"
    	New-Item -ItemType Directory -Path $user.HomeDirectory -Name "Folder2"
    }

    So what did I change and why?

    A major change is in the second line, with two changes:

    • By applying the filter within the AD Command instead of afterwards in a "Where" command, you don't load all AD Users, but only those you actually need.
    • By Specifying the HomeDrive property as to be loaded instead of the wildcard "*", you only load the property you need, not all.

    These two changes speed up your script a lot.

    Thereafter in the loop, I create the folders, by specifying the homedirectory and using the "-name" parameter for the new folders.

    Hope that helped,

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by Mike Laughlin Wednesday, December 4, 2013 5:19 PM
    • Marked as answer by Dayrowshay Wednesday, December 4, 2013 5:55 PM
    Wednesday, December 4, 2013 4:52 PM

All replies

  • Hello Dayrowshay,

    welcome to the world of scripting!

    First of all, a minor edit to your script that ought to make it work:

    # Set timespan
    $fiveminutes = (Get-Date).AddMinutes(-5)
    
    # Grab users.
    $newusers = Get-ADUser -Filter {whenCreated -ge $fiveminutes} -Properties HomeDirectory
    
    # For each user ...
    foreach ($user in $newuser)
    {
    	# Create the two folders
    	New-Item -ItemType Directory -Path $user.HomeDirectory -Name "Folder1"
    	New-Item -ItemType Directory -Path $user.HomeDirectory -Name "Folder2"
    }

    So what did I change and why?

    A major change is in the second line, with two changes:

    • By applying the filter within the AD Command instead of afterwards in a "Where" command, you don't load all AD Users, but only those you actually need.
    • By Specifying the HomeDrive property as to be loaded instead of the wildcard "*", you only load the property you need, not all.

    These two changes speed up your script a lot.

    Thereafter in the loop, I create the folders, by specifying the homedirectory and using the "-name" parameter for the new folders.

    Hope that helped,

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by Mike Laughlin Wednesday, December 4, 2013 5:19 PM
    • Marked as answer by Dayrowshay Wednesday, December 4, 2013 5:55 PM
    Wednesday, December 4, 2013 4:52 PM
  • Wow that's magical!  Thank you so much for the quick response.

    Working perfectly.  I actually can't believe how close I was initially.  Goes to show the importance of proper formatting.

    I appreciate the breakdown for the changes you had.  Helps me understand a little better.

    Thanks again!

    Wednesday, December 4, 2013 5:58 PM