Headers in Powershell are incorrect
Headers in Powershell are incorrect
After importing a CSV file, I create a hashtable of the variables that will be used in a simple command line.
Problem is that when I import the CSV files, the headers are transposed; instead of it being a table with various headers, it is different. My code is:
$Import = Import-Csv "Filepathinput.csv"
foreach ($user in $import) {
$msolparams = @{
UPN = $user.SamAccountName + "@company.com"
Title = $user.JobTitle
Deparment = $user.DepartmentTitle
Displayname = $user.Pname + "," + " " + $user.Plname
Location = $user.location}
Set-MsolUser -UserPrincipalName $msolparams.UPN -Title $msolparams.Title -Department $msolparams.Department -DisplayName $msolparams.Displayname}
After checking the $import
variable with the CSV file input, the hashtable headers come out as so:
$import
SamAccountName : kodak.black
Location : Central
JobTitle : Title
DepartmentTitle : Department
Pname : Kodak
Plname : Black
instead of
SamAccountName Location JobTitle DepartmentTitle Plname Pname
And because of this it does not grab values from the headers properly and gives the error Cannot find MsolUser .
Cannot find MsolUser .
Also you cannot use | ft
anywhere in the variables because the code will error out and will stop.
| ft
Please let me know what you think
this might be useful: stackoverflow.com/questions/42090900/…
– moore1emu
Jun 29 at 16:54
I don't see a problem here. Powershell is showing you a list of properties because it thinks it will not show properly in table format. There is an automatic variable that covers this. That should not be your issue. Can you show some sample data. You could easily just have some whitespace in your columns or something that is causing this.
– Matt
Jun 29 at 16:55
There is no personal info. The name is a dude in a rap group
– AJSKRILLA
Jun 29 at 17:10
Display has got nothing to do with how the powershell object works. I am not an expert on hashtables but I suspect you cannot have more than 2 columns in it, at least not the way you created it.
– Rohin Sidharth
Jun 29 at 19:26
1 Answer
1
I do not see a problem with the code here as presented. Your concern is only actually about the presentation of your data. That has no bearing on its interpretation in PowerShell. I am having, once again, trouble finding the reference to this exact behaviour but you are seeing what PowerShell does on objects with greater than 4 properties. Behind the scenes PowerShell is choosing Format-List
as opposed to Format-Table
. You can simulate this yourself by doing $user | Format-List
Format-List
Format-Table
$user | Format-List
This automation is intended to be a feature so that you can easily read the data on the screen as opposed to it being squished in a table. Horizontal real estate is finite. If you would prefer that view then you can just output it to the console like $user | Format-Table -AutoSize
. Note Please do not save the output of that snippet. It is meant as a display mechanism.
$user | Format-Table -AutoSize
As for your problem, I can only image you are having issues with your input data. Either you have hidden control characters, whitespace, etc. or the error is correct and that user truly does not exist.
I thought this was due to a preference variable but I can't find it. So I thought it was a format.ps1xml but I cannot find the one for pscustomobject.
– Matt
Jun 29 at 19:15
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You might show 2 or 3 lines of your csv file as well. Remove some sensitive information before posting, please.
– Olaf
Jun 29 at 16:53