User Tools

Site Tools


en:powershell:start

PowerShell

Basics

To me, the first thing we need to learn is to deal with is CSV file. First let's read a CSV file, loop across the lines.

Here is an example of CSV input file.

SamaccountnameMail
abc123abc123@domain.com
def123def123@domain.com
ghi123ghi123@test.com
input.csv
Samaccountname,Mail
abc123,abc123@domain.com
def123,def123@domain.com
ghi123,ghi123@test.com
csvLevel1.ps1
Import-Csv -Path input.csv | foreach {
 
    # save current object into a local variable
    $currentLine = $_
 
    # deal with columns
    $mail = $currentLine.Mail
 
    # test mail domain name
    # we add a property with the result of our test
    if ($mail -match "domain.com$") {
        $currentLine | Add-Member -MemberType NoteProperty -Name "domainCheck" -Value "valid"
    } else {
        $currentLine | Add-Member -MemberType NoteProperty -Name "domainCheck" -Value "invalid"
    }
    # return updated currentLine with new column
    $currentLine
} | 
# export result line by line to CSV
# -NoTypeInformation prevents metadata from being exported
Export-Csv -Path preCheck.csv -NoTypeInformation

This script will output CSV file with a new column.

preCheck.csv
"Samaccountname","Mail","domainCheck"
"abc123","abc123@domain.com","valid"
"def123","def123@domain.com","valid"
"ghi123","ghi123@test.com","invalid"
SamaccountnameMaildomainCheck
abc123abc123@domain.comvalid
def123def123@domain.comvalid
ghi123ghi123@test.cominvalid
en/powershell/start.txt · Last modified: 2021/03/16 21:48 by lonclegr