PowerShell Active Directory IF statements

In my organization, we spend a lot of time changing the path to the user's home folders, based on users changing roles, etc. I wrote a very simple powershell script that sets the homedir path from csv .:

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

What I would like to do this is to check users for the presence of -profilepath (roaming profile) and change it (if any) to "$_.HomeDirectory"\Profile(all roaming profiles follow this design). If not, it will simply move on to the next user and do the work as needed.

I played with cameramen ifand really did more harm than good. Has anyone got a life buoy that they can leave me with?

+3
source share
2

, , , "". ProfilePath Get-ADUser. - :

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
}
+1

-Properties ProfilePath, , Get-ADUser .

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

All Articles