You're actually on descent path, there are just a few tweaks you need.
- get rid of the
| ft
.ft
is just an alias forFormat-Table
and allows you to display the output in a table format by default. You only want that for your output, but leave it out of the internal code that way its still an object. - On the
ForEach
, you're already piping in the$permissions
array and process each object, so you can loose the($_.Identity)
- The line 6 you're calling
Select-Object
(select) but not inputting an object, so you're returning$null
. Since you already filtered properties when you imported, you can just return the current object of the ForEach:$_
- As it is, this will just dump the results to the output stream, but it looks like you want to export them to a CSV. You're telling
Export-CSV
where to export it to, but not what to export. The easiest thing is to pipe the ForEach directly to theExport-CSV
(also the -NoTypeInformation gets rid of an annoying pre-amble on the CSV file).
All that being said, this would probably get your script to run as is:
$permissions = Import-csv C:\Users\MyUser\Desktop\Permissions.csv | select Path,Identity
$permissions | ForEach {
$user=Get-ADObject -Identity $_.Identity
if ($user.ObjectClass -eq "user"){
$_
}
} | Export-Csv C:\Users\MyUser\Desktop\groupsRemoved.csv -NoTypeInformation
Another thing you could do is take advantage of the objects you're working with and add a property to your output so you can then do some pivot tables or more filtering:
$permissions = Import-csv C:\Users\MyUser\Desktop\Permissions.csv | select Path,Identity
$permissions | ForEach {
$user=Get-ADObject -Identity $_.Identity
$_ | Add-Member NoteProperty IdentityType $user.ObjectClass
}
$permissions | Export-Csv C:\Users\MyUser\Desktop\identityTyped.csv -NoTypeInformation
If you wanted to, you could also make the property scripted and just have it calculated on the fly. Its okay for a one time output, but might slow things down if you keep going to reference the value:
$permissions = Import-csv C:\Users\MyUser\Desktop\Permissions.csv | select Path,Identity
$permissions | Add-Member ScriptProperty IdentityType { (Get-ADObject -Identity $this.Identity).ObjectClass }
$permissions | Export-Csv C:\Users\MyUser\Desktop\identityTyped.csv -NoTypeInformation
In this instance, you're adding a dynamic property to each object in the $permissions
around that is based on a script and is executed every time the IdentityType
property is asked for by one of the entries.