Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with an PowerShell command to extract user attributes

Posted on 2016-07-21
7
Medium Priority
?
51 Views
Last Modified: 2016-07-26
Hello, what I need to do is get a CSV file with two columns - the first with a list of all users in AD, and the other column with the value of the AD attribute 'userSharedFolder' for that user.

I'm a PowerShell learner.  Here's what I have so far...

Get-ADUser –Filter * -prop cn,userSharedFolder | select cn,userSharedFolder  > \\server\share\IT\Scripts\AD\Ldrive.csv

Now, all the info is there in the resulting CSV, but there are a few formatting issues.  Firstly, the output is not on separate columns in Excel - it's all on the same column, just with loads of white space between the username and the userSharedFolder.  How do I get them onto separate columns, with no white space?  Secondly, I have some usernames that have a leading zero, but Excel sees it fit to remove this digit.  Can you stop this from happening at the script level, or would this be an Excel thing?

Any other tips to improve the above command would be welcomed.

Thanks.
0
Comment
Question by:meirionwyllt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 41

Accepted Solution

by:
footech earned 1200 total points
ID: 41722296
Instead of redirecting to a file with > \\server\share\IT\Scripts\AD\Ldrive.csv (which is equivalent to piping to Out-File), pipe to Export-CSV instead.
Get-ADUser –Filter * -prop cn,userSharedFolder | select cn,userSharedFolder  | Export-CSV \\server\share\IT\Scripts\AD\Ldrive.csv -notype

Open in new window

0
 

Author Comment

by:meirionwyllt
ID: 41722305
Hi, thanks for that.  What I have now are lines of CSV with one cell on each row, in the following format...

cn,"userSharedFolder"

I'm guessing the existence of the comma is a Delimiter issue isn't it.  Am I able to modify the command to make Excel treat this as two columns?  I realise I can do an Insert Object from within Excel but I was just wondering if there is a way to do this from the command.

Also, is there a way of removing the quotes around the userSharedFolder entries?

Thanks.
0
 
LVL 9

Assisted Solution

by:Antzs
Antzs earned 800 total points
ID: 41722821
I have been using the attached script and you could extract any AD Attribute info you need.  Give it a try.

Just need to key in the $path, $SearchBase and $ADServer

Credit to the author who is listed in the powershell script.
Export_AD_Users_to_CSV.v1.0.txt
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 41

Assisted Solution

by:footech
footech earned 1200 total points
ID: 41722860
Export-CSV puts quotes around every field.  This ensures that even if the field includes the same character which is used as the delimiter the field isn't broken up.  When you open the file in Excel you won't see the quotes (when it's parsed properly).
Add the following parameter to Export-CSV.
-Encoding ASCII

Open in new window

Excel doesn't parse the .CSV correctly when the file has certain encodings.  ASCII/ANSI, UTF8 and others work fine.  Unicode and some others don't.  I believe I posted a long time ago the results from testing all encodings, but I don't have them in front of me.

I'm not sure why your instance appears to be generating a file with the encoding that Excel doesn't handle well.  I can't remember if there was a change in different PS versions in this regard.  Or maybe there a difference due to regional settings in Windows.
0
 

Author Comment

by:meirionwyllt
ID: 41722908
Adding the -Encoding ACSII parameter and then using Excel's 'Text to Columns' button (rather than doing an import) has given me what I need, because it gets rid of the quotes and when I select Text rather than General it keeps the leading zero as well.  Thanks.

Anthony - I tried the script you posted but it kept throwing the error...

Select-Object : A parameter cannot be found that matches parameter name 'Path'.
At \\server\share\IT\scripts\AD\Export_AD_Users_to_CSV.v1.0.ps1:95 char:12
+ Export-Csv -Path "$csvfile" -NoTypeInformation
+            ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

As a test I changed line 30 to...

$csvfile = "C:\TG\AllADUsers_$logDate.csv"

just to get rid of any possibility of problems with UNC paths and/or spaces in the path.  But it still gave the same error.

Although I've now been able to achieve what I wanted in my original post, I can see the benefit of using this script in the future so it would be nice to get this working.  Do you know what might be the problem?  I have full permissions to both the UNC path and the local folder.

Thanks.
0
 
LVL 9

Expert Comment

by:Antzs
ID: 41723016
Just change those in the red box.  It should work.

Sript
0
 

Author Comment

by:meirionwyllt
ID: 41723195
Okay I know what's happening here.  I was doing the above three things correctly, but the problem was when I was editing the Select-Object section.  I had to fiddle with this section to add the 'userSharedFolder' label.  In order to keep the resulting spreadsheet clean I hashed out everything I didn't want.  Among the lines that I didn't want was the final 'lastlogondate' line, which has a pipe at the end of it, which is crucial to the script being able to run.

This was my mistake of course, but this script should be slicker.  The '@Label...' entries should all be on their own individual lines, which would allow them to be switched off and on as required.  The same is true for the $_.GivenName entry - you also can't hash this out without breaking the whole script.

But until I figure out how to do that I'll just delete the rows from Excel.
0

Featured Post

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question