0

We have users coming in from other OU. I am creating a script to copy all the necessary attributes in AD. All is okay, except for the homedirectory. The homedirectories are not always on the same server and not always in the same path. So I want to get the homedirectory in AD from the mirror user and replace the username with the incoming user.

\Server1\Users$\username1 (Mirror) to change to \Server1\Users$\username2 ($Incoming)

I created something like this, but it does not do the trick.

#Get usernames of incoming and mirror user
$Incoming = Read-Host -Prompt "Username incoming user"
$Mirror = Read-Host -Prompt "Username mirror user"
#Get Homedir and rename to Homedir Incoming user
$Homedir = Get-ADUser $Mirror -properties Homedirectory |Select-Object -ExpandProperty Homedirectory
$NewHomeDir = $Homedir-Replace('$Mirror', '$Incoming')

I must be overlooking something, but what?

Pisces
  • 1
  • 1
  • 1
    In short: In PowerShell, only `"..."` strings (double-quoted aka _expandable strings_) perform string interpolation (expansion of variable values and expressions), not `'...'` strings (single-quoted aka _verbatim strings_). With `"..."` quoting, if the string value itself contains `"` chars., escape them as `\`"` or `""`, or use a double-quoted _here-string_. See the conceptual [about_Quoting_Rules](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Quoting_Rules) help topic. – mklement0 Jul 03 '23 at 13:18
  • 1
    Also note that you don't need any quoting at all in your case, and that pseudo method syntax (parentheses around the RHS operands) is best avoided. Therefore: `$Homedir -replace $Mirror, $Incoming` – mklement0 Jul 03 '23 at 13:20

0 Answers0