0

I have a CSV text file separated with ; and it's in the format as:

USER_EMPLOYEE_ID;SYSTEM1;USERNAME1

The first column is an identity and the following pairs of columns are user's account on different active directories. I have placed garbage data but the idea is there.

ay7suve0001;ADDPWN;ay7suve0001
AAXMR3E0001;ADDPWN;AAXMR3E0001
ABABIL;ADDPWN;ABABIL
ABDF17;ADDPWN;ABDF17;
ABKMPPE0001;ADDPWN;ABKMPPE0001
ABL1FL;ADDPWN;ABL1FL
AB6JG8E0004;ADDPWN;AB6JG8E0004;
ACB4YB;ADDPWN;ACB4YB
ACK7J9;ADDPWN;ACK7J9
ACLZFS;ADDPWN;ACLZFS;
ACQXZ3;ADDPWN;ACQXZ3

Now there is a requirement that I have to append a fixed string like @ADDPWN.com to all the USERNAME1 values. Some records are having a ; and some don't.

Is there a quick way to append the @ADDPWN.com to each line taking care of:

  • any ;
  • any already @ADDPWN.com

From PowerShell?

1
  • What have you tried? Check out Import-CSV to read CSV and -replace or Replace() to modify a string. Commented Feb 22, 2015 at 14:38

2 Answers 2

1

Import-Csv is your friend. The following should get you on the right track.

Import-Csv "import.csv" -Delimiter ';' | 
foreach { 
    if ($_.username1 -notlike '*@ADDPWN.com') { $_.username1 += '@ADDPWN.com' }
    $_ 
} |
Export-Csv "export.csv" -Delimiter ';'

This assumes the first line of your csv file is your header line. If it's not, you can pass -Header 'USER_EMPLOYEE_ID','SYSTEM1','USERNAME1' as another parameter to Import-Csv.

Export-Csv adds some extra stuff like quotes around parameters, so you may need to play with the output format if you don't want that.

For another explanation how this works, check out Changes last name, first name to first name, last name in last column CSV powershell

Sign up to request clarification or add additional context in comments.

1 Comment

Well, it works. however USER_IDEN;SYSTEM1;USERNAME1; 30;WINDOWS;Wanner.Siegfried; 63;WINDOWS;Ott.Rudolf; 68;WINDOWS;Waldera.Alicja; Becomes #TYPE System.Management.Automation.PSCustomObject "USER_IDEN";"SYSTEM1";"USERNAME1" "30";"WINDOWS";"[email protected]" "63";"WINDOWS";"[email protected]" "68";"WINDOWS";"[email protected]"
1

This was a solution that worked for me.........

#opens list of file names
$file2 ="F:\OneDrive_Biz\PowerApps\SecurityCameraVideoApp\file_list_names.csv"
$x = Get-Content $file2
#appends URl to beginning of file name list
for($i=0; $i -lt $x.Count; $i++){

    $x[$i] = "https://analytics-my.sharepoint.com/personal/gpowell_analytics_onmicrosoft_com/Documents/PowerApps/SecurityCameraVideoApp/Video_Files/" + $x[$i] 
}
$x

#remove all files in target directory prior to saving new list
get-childitem -path C:\_TEMP\file_list_names.csv | remove-item

Add-Content -Path C:\_TEMP\file_list_names_url.csv -Value $x

Comments

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.