[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell - Import User CSV  ignore blanks fields

Posted on 2014-08-26
12
Medium Priority
?
2,744 Views
Last Modified: 2014-08-28
Am trying to import a group of users into powershell - I need to skip some empty fields.  I believed that "" would ignore that field but when I go to AD it actually imports the "".  Any suggestions please.
0
Comment
Question by:BYRONJACKSON
  • 7
  • 5
12 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40285824
Import-Csv file.csv |? { ($_.PSObject.Properties | % {$_.Value}) -ne $null}

Open in new window

0
 

Author Comment

by:BYRONJACKSON
ID: 40285847
cool - will this verify all entries? sorry new to Powershell have over 1000 users to amend
0
 

Author Comment

by:BYRONJACKSON
ID: 40285864
Bascially if I enter null as the value it will import null - if the field is blank I get this error.


Set-ADUser : replace
At line:14 char:3
+   Set-ADUser -Description $($user.Description) -Office $($user.Office) -GivenNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (CN=adamtest4,OU...erange,DC=local:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 29

Expert Comment

by:becraig
ID: 40285883
So you want to filter out where ANY column is blank ?

e.g 5 columns have data  but the sixth does not etc  ?
0
 

Author Comment

by:BYRONJACKSON
ID: 40285903
Yes that is correct - although in some cases there several attributes for users that appear for some but not all users.  So we kind of want something that can sort through each column and add entries where necessary and non entries should appear blank in AD.  Hope this makes sense?

Byron
0
 
LVL 29

Expert Comment

by:becraig
ID: 40285924
Ok so the easiest way I can think of would be a where statement using all the columns or an if statement after the import.

Either would require the names of all the columns in the csv beforehand
(The smarter guys here might have a better approach)
import-csv file.csv | ? {$_.value1 -ne $null -and $_value2 -ne $null -and $_.value3 -ne $null}

Open in new window

OR

import-csv file.csv | % {
if ($_.value1 -ne $null -and $_value1 -ne '' -and $_.value2 -ne $null -and $_value2 -ne '' -and $_.value3 -ne $null -and $_.value3 -ne '')
{#Do something}
}

Open in new window

0
 

Author Comment

by:BYRONJACKSON
ID: 40285964
Thank you will give that a go shortly ... out of interest and for my own learning what does the % represent in the above.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40285971
It means foreach object
? means where object
0
 

Author Comment

by:BYRONJACKSON
ID: 40285985
Thank you very much for that  - will modify what I have and see how I get on. Will let you know soon as

Byron
0
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40286008
Np hope it helps.
0
 

Author Closing Comment

by:BYRONJACKSON
ID: 40289876
Thank you so much! Really helped me out :-)
0
 

Author Comment

by:BYRONJACKSON
ID: 40289878
Thank you so much!
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

OfficeMate Freezes on login or does not load after login credentials are input.
A procedure for exporting installed hotfix details of remote computers using powershell
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Suggested Courses

864 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