Variables from a CSV, ignore the blanks

by Jun 11, 2013


I have written the this script, it is the first stage of clearing out stale AD accounts, I know there are scripts to do this but like the saying goes " give a man a fish, he eats for a day, teach a man to fish, he won't nag you for another one".



get-aduser -filter {description -like "Not logged on for 12 months (30/04/12)"} -searchbase $OU -properties * | export-csv c:description.csv

$openCVS=import-csv -path c:description.csv

$openCVS | ForEach-Object{

    #Create the Variables form the CSV columns




    Write-Host "SamAccountName:"$SamAccoutName " HomeDirectory:" $HomeDirectory "ProfilePath:" $ProfilePath

    Test-Path -path $HomeDirectory

    Test-Path -path $ProfilePath



So my problem is that my CSV file has blanks in it because some AD useres haven't had a home directory or profile directory created.  This means when I run the test-path commands I get

Good Ouput

SamAccountName: educsb  HomeDirectory: \ELHOMEELHOME$educsb ProfilePath: \profilesbprofilesnt$educsb




Bad Output

SamAccountName: dmstest  HomeDirectory:  ProfilePath: \profilesbprofilesnt$dmstest

Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.

At C:cleanout201.ps1:10 char:21

+     Test-Path -path $HomeDirectory

+                     ~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand




This is because the row for dmstest, in the CSV, has no entry under the HomeDirectory column.  So the question is:

How do you skip entry entries in a CSV file when using that column to create a variable?