1

Let's say we have the following code

Get-ADUser -Filter {Enabled -eq $false} -properties * | sort Surname| FT GivenName, Surname, description

It produces:

 GivenName      Surname           description                          
 ---------      ---------         ------- -----------                                              
 Tom            Abbott            AccountingSystems 
 Tim            Baker             AccountingSystems 
 Tyler          Cabot             AccountingSystems

I need to put a space in between 'Accounting' and 'System'

I thought this would work:

Get-ADUser -Filter {Enabled -eq $false} -properties *
% {
   $Description = $.replace("Accounting","Accounting ")
   Set-ADUser = -Description $Description
  }

But... it does not.

2
  • This looks wrong... Maybe more like this $_.Description.replace("Accounting","Accounting ") Commented Feb 3, 2020 at 21:15
  • As an aside: If this answer is correct, Enabled -eq $false doesn't actually work as intended. Does it? Commented Feb 3, 2020 at 22:13

1 Answer 1

1

There are a few things I would recommend you look into,

  1. There is no such variable as $. When you are looping through items with ForEach, you have to use $_ (with underscore)
  2. You cannot assign value to a cmdlet (Set-ADUser = ).
  3. You use | then % (not % | )

Try this,

Get-ADUser -Filter {Enabled -eq $false} -properties Description | % {
   if ($_.Description) {
       $description = $_.Description.replace("Accounting","Accounting ") 
       Set-ADUser $_ -Description $Description
   } # else description is null.
}

Documentation for Set-ADGroup
Documentation for ForEach

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.