User subfolder Creation

Eric Orosz 66 Reputation points
2022-10-03T21:54:16.663+00:00

I would like to create a script to create a new folder called Recover in a new users home folder and using the inherited permissions. I was able to get the it to work by using the following code
$UserName = Read-Host -Prompt 'Enter User ID'
#$FolderName = Read-Host -Prompt 'Input Folder Name'
New-Item "\server\home\PIT\$UserName\Recover" -type directory -Force

but how would I get it to work without prompting for a username is this possible?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-10-04T00:24:17.853+00:00

    Read the user names from a file.

    $AllUsers = Get-Content -Path "C:\data\users.txt"  
    foreach ($UserName in $AllUsers) {   
        "Adding $UserName"  
        New-Item "\\server\home\PIT\$UserName\Recover" -type directory -Force  
    }  
    
    0 comments No comments

  2. Eric Orosz 66 Reputation points
    2022-11-11T04:21:17.617+00:00

    The issue I am having I am trying to create a sub folder called Recover in a new user's home directory after the user created from ADUC but we use Manage Engines AD Manager Plus. The initial home drive user folder is created like normal using the \server\home\site code\%username%. When using the code below the subfolder never creates. Our home share is setup with 10 site code folders then the user folder inside of the parent. I don't understand what I would have to put in my code for it to read or know what site code\$username for the Subfolder Recover to auto create.
    New-Item "\server\home\PIT\$UserName\Recover" -type directory -Force


  3. MotoX80 36,291 Reputation points
    2022-11-15T15:55:34.227+00:00

    I do not have an AD environment to test with, so you will need to get the Get-ADUser command working for your environment.

    [Cmdletbinding()]  
    Param(  
        [string]$UserName = ""  
    )  
      
    $LogFile = "C:\temp\RecoverFolderCreation.log"   
      
    function LogIt($msg){  
        $msg                            # display if we are invoked interactively.  
        "$(get-date) - $msg" | Out-File $LogFile -Append  
    }   
       
    try {  
        if ($UserName -eq "") {  
            LogIt "No user name parameter was provided."   
            return  
        }  
        LogIt "RecoverFolderCreation invoked for $username"   
        $user = Get-ADUser -Identity $UserName -Properties sAMAccountName,HomeDirectory  
        $FolderName = $user.HomeDirectory + "\Recover"  
        LogIt "Creating $FolderName"   
        New-Item $FolderName -type directory -Force  
      
    } catch {  
        LogIt "We crashed."  
        LogIt $_   
    }  
      
      
      
    
     
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.