Create HomeDrive (Personal drive/folder) for selected users in Out-GridView

Hello!

On beforehand, I will tell you that I am a beginner in PowerShell and only have some experience in “Get-…” commands, not so much more than that.

Im trying to create a powershell script where I can search for AD users created X days ago and select those who dont have a HomeDrive/HomeDirectory yet… This is how long i’ve come so far and cant seem to figure out how to create the homedirectory for the users I have selected from the Out-GridView…

(Note: In the Read-Host when running the script I put in 40 days and select 5-6 users out of aprox. 35, and want to create their own homedrive (a personal drive))

$search = Read-Host -Prompt 'How many days ago should the user have been created?'
$date = (get-date).Tostring()
$month = (Get-Date).AddDays(-$search)
Get-ADUser -Filter * -Properties whencreated,Name,HomeDirectory,HomeDrive, SamAccountName | 
where { $_.whenCreated -ge $month } | select SamAccountName, Name, HomeDrive, HomeDirectory | Out-GridView -PassThru

How do I go from here?

I have this line that I thought was useable but I cant figure out how and where to put it…

set-aduser gbaldwin -homedirectory \\01file003\Personlig$\%username% -HomeDrive p:

Dont really know the reason for ‘gbaldwin’, I found it online, so if anyone could explain, it would be great! :slight_smile:

 

Thanks in advance!

-Tobias H /(Obliwobbi)

If you want to map values to specific parameters, here is a basic example:

Get-Service | 
Where {$_.Status -eq 'Running'} |
Select -First 5 | 
Out-GridView -PassThru | 
foreach { 
    Restart-Service -Name $_.Name -WhatIf
}

The for loop can even be omitted if the command you are passing to accepts pipeline ByValue or ByPropertyName (e.g. Name maps to Name):

    -Name <String[]>
        Specifies the service names of the services to restart.
        
        Required?                    true
        Position?                    0
        Default value                None
        Accept pipeline input?       True (ByPropertyName, ByValue)
        Accept wildcard characters?  false

You can simple do:

Get-Service | 
Where {$_.Status -eq 'Running'} |
Select -First 5 | 
Out-GridView -PassThru | 
Restart-Service -WhatIf

Home directory things don’t work very well when setting them with powershell. What you have created for the dynamic “how old is this account” thing is fine. You don’t need the “| Out-GridView -Passthru” piece, that will just make you a window that you can filter and observe.

This will show you all of the users with home directories…

get-aduser -filter * -Properties homeDirectory,homeDrive | where {$_.HomeDirectory -ne $null} | select name,homeDirectory,homeDrive | sort name

And technically, this will set their home directory (but this does not work)

set-aduser -homeDirectory "\\Path\To\Home\%username%" -HomeDrive H;

Now, the problem is that when you open the user properties in the AD Users and Computers tool and select the profile tab, if you bullet “connect”, then pick the drive letter and paste in the value “\Path\To\Home%username%”, and click “Apply”. This will check the path and update the username to the actual SamAccountName, and it will create an empty folder at the path. Next user login, the user will have a drive mapped that is empty and only they have permission to it (if you’ve set it all up correctly). The process of resolving the username and creating the folder on the home share does not happen if you set this value through powershell.

Since you will need to do this manually anyway (unless someone else can suggest a way that actually works), you should only want to filter the results to show you which accounts DON’T have the home drive, that were created within the days you’re checking for. Change your Where-Object to look like this…

| where {($_.whenCreated -ge $month) -and ($_.HomeDirectory -eq $null)}

 

And technically, this will set their home directory (but this does not work)


It might work if you specify the -Identity, and %username% means nothing to PowerShell
Set-ADUser -Identity $UserName -HomeDirectory "\\Path\To\Home\$UserName" -HomeDrive 'H'

[quote quote=206130]And technically, this will set their home directory (but this does not work)

<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
It might work if you specify the -Identity, and %username% means nothing to PowerShell
Set-ADUser -Identity $UserName -HomeDirectory "\\Path\To\Home\$UserName" -HomeDrive 'H'
[/quote]

I’ve also tried that before with the same result. Whatever the ADU&C tool does when you click Apply/OK after setting those properties is not being done when powershell sets those values.

It works for me :slight_smile:

[quote quote=206340]It works for me :slightly_smiling_face:

[/quote]
That is good! The last directory I tried it on was at a 2012 function level. Could that be the difference?