0

When I run my code, it gives me the following error. Error What I am having the code do is taking the csv then filtering the name column to only be administrators, then I am having the caption column filter all of the operating systems that I have provided. The if statement is saying that If the column Type0 equals domain then put in the "Unique Account Name" column the Domain0 following the Account0. Else it will put it in Netbios name and then Account0. Below will be the excel sheet. and the desired output of each. Excel Sheet Desired output of Unique Account Name

 Import-Csv 'U:\Local Group Members.csv' | where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded" |
ForEach-Object 

If ($_.Type0 -eq 'Domain') { 
    $_.UniqueAccountName = "$($_.Domain0) - $($_.Account0)" 
} Else { 
    $_.UniqueAccountName = "$($_.Netbios_name0) - $($_.Account0)" 
}

Export-Csv -notypeinformation U:\LGMbestone.csv

I am a powershell Novice, and I am stuck, and how do I can I get this code to run?

8
  • Possible duplicate of If statement CSV Powershell Commented Jun 14, 2017 at 14:18
  • It seems unlikely that repeatedly asking the same question multiple times without sufficient detail isn't going to get you an answer. Commented Jun 14, 2017 at 14:19
  • @Bill_Stewart does this one not give more detail than the other one? Commented Jun 14, 2017 at 14:20
  • @Bill_Stewart This one is a different question. This one is about the Unique Account Name. Commented Jun 14, 2017 at 14:22
  • It's very, very close to the same thing. Commented Jun 14, 2017 at 14:23

2 Answers 2

2

you can't pipe to an if statement, and you're going to need to iterate through the result line by line to modify the object, which calls for a foreach loop. Also, piping to Export-CSV is better than calling it with an input object. An example based on your code is below. Hope this helps!

    $csv = Import-Csv 'U:\Local Group Members.csv' |
Where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded"

#Modify each line based on your parameters
Foreach ($row in $csv) {
    If ($row.Type0 -eq 'Domain') { 
        $row."Unique Account Name" = "$($row.Domain0) - $($row.Account0)" 
        Write-Host $row."Unique Account Name"
    } Else { 
        $row."Unique Account Name" = "$($row.Netbios_name0) - $($row.Account0)"
    }
}

#Export CSV
$csv | Export-Csv C:\LGMbestone.csv -NoTypeInformation
Sign up to request clarification or add additional context in comments.

8 Comments

I ran that code and it ran all for me except it did not give me the export of the csv
That seemed to work after i switched the path. But now The code works only for the 'Domain' part.
@Sprengdilly Try changing $row."Unique Account Name" to $row.UniqueAccountName. It has to match the column name in the CSV, so if there are no spaces in the CSV, remove the spaces in the script. Let me know if that helps.
Unfortunately that did not help, The Domain part worked as I stated before, so I do not believe it has anything to do with the "Unique Account Name"
I double checked all of the spelling to on everything and it still appears to not work on the Local ones. Would doing another if statement be able to fix this problem?
|
0

looks like one formatting issue. With foreach , it should be something like this:

$import=Import-Csv 'U:\Local Group Members.csv' | where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded" 

ForEach($imp in $import)
{ 

    If ($imp.Type0 -eq 'Domain') 
    { 
        $imp.UniqueAccountName = "$($imp.Domain0) - $($imp.Account0)" 
    } 
    Else 
    { 
        $imp.UniqueAccountName = "$($imp.Netbios_name0) - $($imp.Account0)" 
    }

    Export-Csv "$imp.UniqueAccountName" -notypeinformation "U:\LGMbestone.csv" -Append
}

Note: I have not checked the import-csv part. I believe its properly taking the data and you should use foreach loop for iterating each row value

7 Comments

You can't pipe to if.
typo :).removing it
@RanadipDutta can you show what you mean by using the foreach loop and iterating each row value?
@RanadipDutta this is probably a stupid question but what is the -append for at the end of the code?
-Append is used for appending the data in the file. Example: YOu have already data1 in a file. And you want to add data2 to your file keeping the data1. Thats appending. So in this case, each data whatever coming out of the loop is getting appended (adding the additional values each time instead of replacing the file)throughout the loop.
|

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.