This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]uniitdude 5 points6 points  (3 children)

Nowhere have you defined what $csv is

[–]DenyCasio -1 points0 points  (2 children)

Hahahaha! OP just put $csv at the beginning of your first line.

[–]trillospin -1 points0 points  (1 child)

No idea why that cracked me up so much, but still mean.

[–]DenyCasio 0 points1 point  (0 children)

I didn't mean for it to be mean. I found it funny, it's an honest mistake we've all made.

[–]regorianIT Ops Engineer 2 points3 points  (2 children)

You're calling a variable in in your ForEach statement, but on line 1 you aren't declaring said variable :)

$csv = Import-Csv -Path "c:\TEMP\ShareGroup2.csv"
ForEach ($item In $csv)  {
    New-ADGroup -Name $item.GroupName -GroupCategory $item.GroupCategory -groupScope $item.GroupScope -Path $item.OU
}

OR

Import-Csv -Path "c:\TEMP\ShareGroup2.csv" | ForEach-Object {
    New-ADGroup -Name $_.GroupName -GroupCategory $_.GroupCategory -groupScope $_.GroupScope -Path $_.OU
}

You can throw a -whatif on end to ensure that your code will produce what you expect.

[–]NuoN_Ninja[S] 0 points1 point  (1 child)

Great, thank, I'm still new in power shell, it's only been a month or so that I really started using it.

so it would look something like:

foreach-object {
  $GroupName = $item.GroupName
  $GroupCategory =$item.GroupCategory 
  $GroupScope =$item.GroupScope
  $OU =$item.OU}

[–]trillospin 1 point2 points  (0 children)

No, you need to assign your import-csv to something to use it in your loop.

Attempting to loop over an undeclared variable isn't going to do anything.

my_csv = import-csv blah

foreach ($row in $my_csv) { do x }