none
Changing AD home directory entry via regex RRS feed

  • Question

    • I need to substitute a server address with a DFS one in each user's home directory. Unfortunately only a portion of the string is constant. Bellow is an attempt in Powershell that works but is clumsy. Is there a better way?

      $hdir = (Get-ADUser bguest -Properties *).HomeDirectory
      $hdir = $hdir -replace "\\\\.*\\", "\\\\ad.company.com\\lfl\\"
      $hdir = $hdir -replace "\\", "-"
      $hdir = $hdir -replace "--", "-"
      $hdir = $hdir -replace "-", "`\"
      Set-ADUser bguest -HomeDirectory $hdir

      Any pointers are greatly appreciated

    Monday, December 7, 2015 6:18 PM

Answers

  • Here's a short way to change the server part of a UNC path:


    PS C:\> '\\server1\share 1\directory 1' -replace '^\\\\([^\\]+)', '\\server2'
    \\server2\share 1\directory 1
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by KGN-Jack Tuesday, December 8, 2015 12:22 PM
    Monday, December 7, 2015 10:35 PM
    Moderator

All replies

  • Use split.

    $parts = $hdir.Split('\')


    \_(ツ)_/

    Monday, December 7, 2015 7:32 PM
  • THis builds a new path:

    $parts = $hdir.Split('\',[System.StringSplitOptions]::RemoveEmptyEntries)
    $parts[0]='NewserverName'
    [io.path]::Combine('\\',$parts)
    


    \_(ツ)_/

    Monday, December 7, 2015 7:41 PM
  • Here's a short way to change the server part of a UNC path:


    PS C:\> '\\server1\share 1\directory 1' -replace '^\\\\([^\\]+)', '\\server2'
    \\server2\share 1\directory 1
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by KGN-Jack Tuesday, December 8, 2015 12:22 PM
    Monday, December 7, 2015 10:35 PM
    Moderator
  • Thanks. That works like a charm.
    Now I have to puzzle out the regex '^\\\\([^\\]+)'
    Tuesday, December 8, 2015 12:23 PM