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…
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
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)
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)}
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.