Return the value of homedrive/homedirectory for specific user

Hello!

I'm an extreme newb when it comes to all things PowerShell. Most of my use has come through Google-Fu.

Due to all this COVID-19 fun, we are supporting a larger amount of teleworkers. For the entity I work for, different teams manage different agencies/OUs. One particular OU utilizes .kix scripts for login. As you can imagine, when a user logs in to their machine, they are not connected to VPN and therefore the script can't map network shares.

What I am trying to accomplish is find a way to query the logged in user's homedrive/homedirectory so I can output that as a variable to be called in a post-VPN manual execution of the .kix. The .kix maps shared drives for the agency, so before I call the .kix I'd like to programmatically map the home drive of the current user, without having to explicitly define each user in a unique instance of the file.

Here is what I tried in PowerShell, with a couple variants, always giving me parameter/argument errors...

get-aduser -filter SamAccountName -like $env:username -properties homedrive, homedirectory

-EDIT-

I think I am getting closer by assigning the environment variable to my own variable, and calling that variable in the following:

$cU = $env:username
Get-ADUser -Filter {name -like $cU} -Properties homedrive, homedirectory

But now, the line seems to execute but provides no feedback, error or return value...

-Edit-

...And that's because there isn't anything being returned... I had the return written to file, file is blank. Cool...

-EDIT-

Closer yet...

I need to figure out how to trim this data now...

$cU = $env:username
Get-ADUser -Filter {SamAccountName -like $cU} -Properties SamAccountName, HomeDrive, HomeDirectory | Out-File -FilePath .\Test.txt -NoClobber

In case anyone is wondering, stumbling through my stumblings... apparently if you want to filter on a property, you also need to declare that in the Properties... (correct me if I am wrong). The FilePath for Out-File with the way I've annotated it just basically places that text file where the script line is already pointing (current directory). NoClobber just means don't overwrite if it exists.

Parents
  • Well, that was fun...

    $cU = $env:username
    Get-ADUser -Filter {SamAccountName -like $cU} -Properties SamAccountName, HomeDrive, HomeDirectory | Select-Object SamAccountName, HomeDrive, HomeDirectory

    That's what I was looking for. Hopefully my foolishness digging around in the dark helps someone else find the light a lot quicker.

Reply
  • Well, that was fun...

    $cU = $env:username
    Get-ADUser -Filter {SamAccountName -like $cU} -Properties SamAccountName, HomeDrive, HomeDirectory | Select-Object SamAccountName, HomeDrive, HomeDirectory

    That's what I was looking for. Hopefully my foolishness digging around in the dark helps someone else find the light a lot quicker.

Children
No Data