Basic Powershell and home drives

Associate
Joined
6 Jun 2005
Posts
1,856
Location
Cambridge
Hi all,

I'm fairly new to powershell but I'm attempting to write a query to modify the home drive and location for a user. I've done this before and it's worked fine, so I'm not quite sure what I'm doing wrong. Apologies if its blatantly obvious.

So, my command:

dsquery user -name "joe bloggs" | dsmod -hmdrv Z -hmdir "\\File01\User Drives\$username$"

I've seen examples on the internet of people using both $username$ and %username% as tokens.

if I use $username$ token and open up ADUC and locate "joe bloggs" I see the following:

Home Folder - Mapped to Z: with the path \\File01\User Drives\$

and with the %username% token:

Home Folder - Mapped to Z: with the path \\File01\User Drives\%username%

If I add a white space or delete a character, re-add it, and press apply using the %username% token, it will resolve it to the username correctly.

Anyone got any ideas?

Regards,

David
 
Hi David,

%username% seems to only work from the UI, or at least, doesn't work with powershell in my experience.
What you can do is thus:

Code:
$username = "joe bloggs"
dsquery user -name "$username" | dsmod -hmdrv Z -hmdir "\\File01\User Drives\$username"

Assuming you want to run this on multiple users?

Code:
Import-CSV userlist.csv | ForEach-Object {
$username = "$_.username"
dsquery user -name "$username" | dsmod -hmdrv Z -hmdir "\\File01\User Drives\$username"
}

this will work if you have a username column in your CSV, $_ being the current pipeline object, and .username defining the correct part.

If your likely to be doing this kind of thing regularly, I can highly recommend the (free) Quest AD Tools for powershell

I've created scripts to create & adjust our user groups every school year which work pretty well.
 
Back
Top Bottom