Hello,
I am trying to use the Add-Content Cmdlet to add variables input via Read-Host to an existing .csv file. I can get it working to an extent. The problem i have is that the variables don’t get appended to the correct column. Instead, each variable gets added to the first column in the .csv.
Function Input-UserDetail {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$Username,
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$FirstName,
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$Surname,
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$DispName,
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$Desc,
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$Title,
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$Depar,
[Parameter(ValueFromPipelineByPropertyName = $true)][string]$SamName,
[Parameter()][String]$Path = "C:\Holding\Scripts\Starter&LeaverScripts\NewADUser.csv"
)
$Username = Read-Host "Enter Full name of new staff member (e.g. John Smith, Lady Grey, George Bush)"
$FirstName = Read-Host "Enter First name of new staff member"
$Surname = Read-Host "Enter Surname of new staff member"
$DispName = $Username
$Desc = Read-Host "Enter Job title of new staff member"
$Title = Read-Host "Enter Title of new staff member (Mr, Mrs, Miss etc)"
$Depar = Read-Host "Enter Department of new staff member"
$SamName = Read-Host "Enter Username name of new staff member in the format first initial and surname (e.g jsmith, lgrey, gbush)"
Get-testCSV
ForEach-Object{
$_.Username
$_.FirstName
$_.Surname
$_.DispName
$_.Title
$_.Depar
$_.SamName
}
Add-Content -Path $Path -Value $Username,$FirstName,$Surname,$DispName,$Desc,$Title,$Depar,$SamName
}
This is my latest attempt. I have also tried these methods:
Add-Content -Path $Path.Username -Value $Username
Where-Object $_.Username | Add-Content -Path $Path -Value $Username
Add-Content -Path $Path | Where-Object {$_.Username } | Set-Content $Username
-Path $Path | Where-Object {-Value 'Username'} | Input-UserDetail -Username $_.Username
They all get the same result as shown in the attatched .csv.
Any help greatly appreciated!