Wednesday, October 29, 2008

Extract UPN from text file

#####################################################
####### extract-UPN.ps1
####### ::Tarun Arora
####### v1.0
####### This script will extract any UPN ending with $upn(variable below) from a text file
#######
####### Known Issue - If there is a email like user@UPN.com.au, it will extract that as well (not tested)
######################################################



## Update the relevant output file (containing the UPN's)
$outputFile = "c:\scripts\tarun\sortdata\UPN1.txt"

## Update the $ContentPath to the file you want to extract the UPN's
$contentPath= "c:\scripts\tarun\sortdata\content.txt"

## UPN suffix
$upn="@ABC"


################################################
## No need to change anything below this line
##################################################
$content = get-content $contentpath

write-output "UPN" | out-file $outputFile

$word=""
foreach ($line in $content)
{

$len=$line.length

if($len -gt 3)
{
for ($j=0; $j -lt $len;$j++)
{
$i = $line.substring($j,1)

$word=$word+$i
if($word.length -gt 5)
{
if($word.substring($word.length - 4,4) -ilike $upn){write-output $word | out-file $outputfile -append}

}

if(($i -eq " ") -OR ($i -eq [char]10) -OR ($i -eq [char]13) -OR ($i -eq " ")){$word=""}

}
$word=""
}
}