Power Shell Script for output

i am having a csv file with user's Display Name and i need a powershell script to enable all the accounts which is in the csv file and get an output in csv
abdull sheriffSystems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FOXActive Directory/Exchange EngineerCommented:
Make sure the heading above the displaynames reads    DisplayName
Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv 'c:\temp\results.csv' -notypeinformation
abdull sheriffSystems EngineerAuthor Commented:
i have a csv file with the heading as DisplayName and below them i have the users displayname listed

when i run, i get the below error

Get-ADuser : Cannot find an object with identity: 'Abdull Sheriff' under: 'DC=123,DC=4567,DC=com'.
At line:2 char:4
+ % {Get-ADuser $_.DisplayName | Enable-AdAccount} |
+    ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Abdull Sheriff:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetAD
   User
FOXActive Directory/Exchange EngineerCommented:
That is basically stating that it doesn't find that user Abdull Sheriff in that domain.  
1.Are you in a multi domain environment?  
2.Are you sure that user is sitting in that domain?
3.Was that user actually created already
4. Verify the users display name is matching from AD to your .csv file.
SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

abdull sheriffSystems EngineerAuthor Commented:
1.Are you in a multi domain environment?  NO
2.Are you sure that user is sitting in that domain? YES
3.Was that user actually created already YES
4. Verify the users display name is matching from AD to your .csv file. Correct Display names

STILL Error, let me explain in this way
 

i have a csv file name DisplayName.csv , i have attached the csv file for your refernce

First Name: Abdull , Last Name: Sheriff, Display Name: Abdull Sheriff - Accounts India
First Name: Peter  , Last Name: Paul, Display Name: Peter Paul - Finance Asia
First Name: John , Last Name: Paul, Display Name:  - IT Japan

All the display name have their county and department mentioned so my csv file have those as the display name
Now i run the below command (which you gave)

Import-Csv c:\displaynameIN.csv | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv c:\displaynameout.csv -notypeinformation

I get the same error as before. And i am running this command in the AD server
FOXActive Directory/Exchange EngineerCommented:
please attach .csv, upload to experts-exchange and then submit. I don't see it here.
abdull sheriffSystems EngineerAuthor Commented:
this is how it looks when you open displaynameIN.csv

DisplayName
Abdull Sheriff - Accounts India
Peter Paul - Finance Asia
John Paul - IT Japan
FOXActive Directory/Exchange EngineerCommented:
please put opening and closing quotes for each DisplayName in your csv file.

 Ask a Question

Experts Exchange > Questions > Power Shell Script for output
?
Power Shell Script for output
Posted on 2018-04-19
Powershell
6
Low Priority ?
32 Views
Last Modified: 2018-04-30
i am having a csv file with user's Display Name and i need a powershell script to enable all the accounts which is in the csv file and get an output in csv
 Comment Report Question
Question by:
abdull sheriff
 
Collapse Question
6 Comments
Your Comment

by:FOX
Active Directory/Exchange Engineer
ID: 42538602
2018-04-19
Make sure the heading above the displaynames reads    DisplayName
Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv 'c:\temp\results.csv' -notypeinformation
Report Comment
Author Comment

by:abdull sheriff
ID: 42539487
2018-04-20
i have a csv file with the heading as DisplayName and below them i have the users displayname listed

when i run, i get the below error

Get-ADuser : Cannot find an object with identity: 'Abdull Sheriff' under: 'DC=123,DC=4567,DC=com'.
At line:2 char:4
+ % {Get-ADuser $_.DisplayName | Enable-AdAccount} |
+    ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Abdull Sheriff:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetAD
   User
Report Comment
Your Comment

by:FOX
Active Directory/Exchange Engineer
ID: 42539822
2018-04-20
That is basically stating that it doesn't find that user Abdull Sheriff in that domain.  
1.Are you in a multi domain environment?  
2.Are you sure that user is sitting in that domain?
3.Was that user actually created already
4. Verify the users display name is matching from AD to your .csv file.
Report Comment
Author Comment

by:abdull sheriff
ID: 42549429
56m
1.Are you in a multi domain environment?  NO
2.Are you sure that user is sitting in that domain? YES
3.Was that user actually created already YES
4. Verify the users display name is matching from AD to your .csv file. Correct Display names

STILL Error, let me explain in this way
 

i have a csv file name DisplayName.csv , i have attached the csv file for your refernce

First Name: Abdull , Last Name: Sheriff, Display Name: Abdull Sheriff - Accounts India
First Name: Peter  , Last Name: Paul, Display Name: Peter Paul - Finance Asia
First Name: John , Last Name: Paul, Display Name:  - IT Japan

All the display name have their county and department mentioned so my csv file have those as the display name
Now i run the below command (which you gave)

Import-Csv c:\displaynameIN.csv | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv c:\displaynameout.csv -notypeinformation

I get the same error as before. And i am running this command in the AD server
Report Comment
Your Comment

by:FOX
Active Directory/Exchange Engineer
ID: 42549447
37m
please attach .csv, upload to experts-exchange and then submit. I don't see it here.
Report Comment
Author Comment

by:abdull sheriff
ID: 42549473
21m
this is how it looks when you open displaynameIN.csv

DisplayName
"Abdull Sheriff - Accounts India"
"Peter Paul - Finance Asia"
abdull sheriffSystems EngineerAuthor Commented:
i tried both ' and " 
still the same :(

Cannot find an object with identity: 'Abdull Sheriff' under: 'DC=123,DC=4567,DC=com'.
At line:2 char:4
FOXActive Directory/Exchange EngineerCommented:
Is that the only user erroring out?

Please run the below command and paste the results.  You must have some type of typo in the .csv file

Get-Aduser -properties * -filter "Surname -like 'Sheriff'" | Select CN,DisplayName,SamaccountName
abdull sheriffSystems EngineerAuthor Commented:
Abdull Sheriff            Abdull Sheriff - Accounts India           ASheriff
FOXActive Directory/Exchange EngineerCommented:
Is that the only account that is throwing the error?

Your displayname under the DisplayName header in the .csv should be as above   "Abdull Sheriff - Accounts India"
If this command found it there is something wrong with your .csv.  Remove the entry in your .csv and readd it.  Make sure there are no spaces after the closing colon.  There may be a space somewhere and when the command is pulling the name it is not recognizing it.
abdull sheriffSystems EngineerAuthor Commented:
no for all the users its happening, i have checked the spelling and spaces all are correct but still the same error.

ok can you give me a script for email address rather than display name , let me try that with emailaddress in the csv
FOXActive Directory/Exchange EngineerCommented:
Let us use the samaccountname and NOT the emailaddress if we are enabling the accounts.
At the top of the .csv column with the samaccountnames name it Samaccountname

Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-ADuser $_.Samaccountname | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv 'c:\temp\results.csv' -notypeinformation
abdull sheriffSystems EngineerAuthor Commented:
hey Fox, Looks like script works when we try with samaccountname .
Once i run the script i can see the accounts are getting enabled BUT in the c:\temp\results.csv output file is 0 kb and no data(just blank) can been seen.
I need something which tells the accounts are enabled in the output file
abdull sheriffSystems EngineerAuthor Commented:
hey Fox, Looks like script works when we try with samaccountname .
Once i run the script i can see the accounts are getting enabled BUT in the c:\temp\results.csv output file is 0 kb and no data(just blank) can been seen.
I need something which tells the accounts are enabled in the output file
Can we make this happen ???
FOXActive Directory/Exchange EngineerCommented:
Fair enough- Since we know the accounts are already enabled and you just need a report -

Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-Aduser $_.Samaccountname -properties * | Select Samaccountname, DisplayName,enabled} | Export-Csv 'c:\temp\results.csv' -notypeinformation
abdull sheriffSystems EngineerAuthor Commented:
when i run the below script the accounts get enabled , cant the output file SamACCOUNT.csv have the enable information rather than just a blank file ? i dont want to run a different script just to get the report

Import-Csv c:\SamACCIN.csv | %{Get-ADuser $_.Samaccountname | Enable-AdAccount} |
Select DisplayName,samaccountname,enabled | Export-Csv c:\SamACCOUT.csv -notypeinformation
FOXActive Directory/Exchange EngineerCommented:
run the below

Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Enable-AdAccount} |
Select DisplayName,samaccountname,enabled | Export-Csv 'c:\SamACCOUT.csv' -notypeinformation
abdull sheriffSystems EngineerAuthor Commented:
after running the below the accounts get enabled but the outputfile SAMACCOUT is blank and 0kb still :(

Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Enable-AdAccount} |
Select DisplayName,samaccountname,enabled | Export-Csv 'c:\SamACCOUT.csv' -notypeinformation
FOXActive Directory/Exchange EngineerCommented:
Abdull,
Download the below file, rename the extension to .ps1 so that it reads EnableAccts.ps1, then save it somewhere like  c:\temp
Open powershell (run as administrator)
cd to c:\temp or wherever you save the file
Once you cd to the directory where the file is saved type in     .\EnableAccts.ps1 and press enter

(you may right-click the .ps1and edit to view the commands)  Very simple- I put it in 2 lines.
EnableAccts.txt

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abdull sheriffSystems EngineerAuthor Commented:
awesome the script worked in power shell ISE itself ,
why are we saving it as EnableAccts.ps1 and then calling it again in powershell when this script works directly in power shell ISE ?
FOXActive Directory/Exchange EngineerCommented:
you copied and pasted it into powershell ise. I could not upload the script as a .ps1 to EE so I had to rename it to a .txt just for the upload. Yes it works...in it's native form it has to be renamed  back to EnableAccts.ps1
abdull sheriffSystems EngineerAuthor Commented:
i was wondering why you created the enableAccts.psi instead you could have just copied the scripts

Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Enable-AdAccount}
Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Select DisplayName,samaccountname,enabled} |  Export-Csv 'c:\SamACCOUT.csv' -notypeinformation

like this here in EE comment session.

Since you did .ps1 i was wondering what could be the reason
FOXActive Directory/Exchange EngineerCommented:
abdull,
If you open up your shell and go to the directory where you saved EnableAccts.ps1 all you have to do is type .\EnableAccts.ps1 and press enter.   It is just the script wrapped into one name instead of you copying and pasting lines.
abdull sheriffSystems EngineerAuthor Commented:
thank you so much FOX
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.