2

I'm currently trying to get to grips with powershell and on a previous script I made i was doing a couple of silly things, editing values of text files, only to tell powershell to get those values again. I've stayed up late last night watching tutorials but was struggling to find a method for this, and was looking for some help in the correct way to do the below

The Aim

Get the script to edit the value of a variable, the variable includes the home directory properties of a test account

What i have currently

{
Do {

    $name = Read-Host 'Enter the required Log on ID'

} Until ($name)

$drive = Get-ADUser $name -Properties * | select homedirectory
$drive = $drive -replace 'homedirectory', '' 
$drive

The initial variable is

homedirectory                                                                                                               
-------------                                                                                                               
\\WIN-7V7GI0R7CFK\homedrives\Onetest$

And after using the replace funtion it turns into

@{=\\WIN-7V7GI0R7CFK\homedrives\Onetest$}

Please see the attached screenshot, I am looking for the $drive variable to just be the drive path, which in this case would be \WIN-7V7GI0R7CFK\homedrives\Onetest$

Any help is appreciated, just looking for the correct method in tying all this together so i can incorporate this into other parts of the script if necessary

Screenshot

mklement0
  • 382,024
  • 64
  • 607
  • 775
sheady
  • 27
  • 4

1 Answers1

3

When you do select homedirectory, you are retrieving an object with one single property called homedirectory.

If you want the value of that property only, use

Select-Object -ExpandProperty HomeDirectory

No need to do any funky replace actions afterwards.

P.S.

  1. It is considered bad practice to use -Properties * unless you indeed need ALL the properties of a user. In this case, you are only interested in one of them, so the code would improve if you change that to -Properties HomeDirectory.

  2. Reading the wanted user with Read-Host and then using that as Identity parameter for the Get-ADUser cmdlet is asking for exceptions.. The Identity can only be a SamAccountName, a DistinguishedName, the GUID or the users SID. Either make that clear in the prompt for Read-Host or use the -Filter parameter instead, so you can capture errors and report back to the user.

Theo
  • 57,719
  • 8
  • 24
  • 41