user3333923
user3333923

Reputation: 33

PowerShell Active Directory IF statements

In my organization, we spend a large amount of time changing user home folder paths, based on users changing roles, etc. I have written a very simple powershell script that sets the homedir path from a csv.:

Import-Csv C:\Dir\homedir.csv | ForEach-Object {
    Set-ADUser $_.SamAccountName –HomeDirectory $_.HomeDirectory
}

What I would like for this to also do, is to check the users for the presence of a -profilepath (roaming profile) and change that (if present) to "$_.HomeDirectory"\Profile (all roaming profiles follow this construct). If not present, it would simply skip down to the next user and perform the work as required.

I've played around with if statements, and really done more harm than good. Anyone got a lifeline they can throw me?

Upvotes: 3

Views: 1205

Answers (2)

rtf
rtf

Reputation: 734

Hard to tell where you're getting stuck, but I assume it's in the 'properties' bit. ProfilePath is not pulled by default when you issue the command Get-ADUser. Something like this should do the trick:

Import-Csv C:\Dir\homedir.csv | ForEach-Object {

    $user = Get-ADUser $_.SamAccountName -Properties 'ProfilePath'

    if($user.ProfilePath -ne $null)
    {
        $profilepath = $_.HomeDirectory + "\Profile"
        Set-ADUser $user –ProfilePath $profilepath
    }
    Set-ADUser $_.SamAccountName –HomeDirectory $_.HomeDirectory
}

Upvotes: 1

Guvante
Guvante

Reputation: 19213

You need to specify -Properties ProfilePath if you want the Get-ADUser command to return the profile path.

$user = Get-ADUser $_.SamAccountName -Properties ProfilePath
If ($user.ProfilePath -ne $null) {
    Set-ADUser $_.SamAccountName -ProfilePath "$($_.HomeDirectory)\Profile"
}

Upvotes: 1

Related Questions