NYPkgFellos
NYPkgFellos

Reputation: 27

Match a string against a file with list of strings to take action in powershell

I am looking to match a name against a text file and want to remove few things if name is not found in entire file. I tried foreach but when checking against a list of 10 names and 9 dont match, it is running 9 times to remove. I would like it to just check entire file against one name provided and remove if name does not match.

$UIDsInFile = Get-Content -Path ".\Data\UserIDs.txt" -ErrorAction SilentlyContinue
foreach ($un in $UIDsInFile)
{
if ($un -eq $llusername)
{
    RemoveAllAddins
    InstallAddin
}
}

if ($UIDsInFile -notcontains $llusername)
{
    RemoveAllAddins

}

Upvotes: 0

Views: 58

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36287

It really looks like you already have what you want in your second If statement. A simpler version would be:

#Load list of user names
$UIDsInFile = Get-Content -Path ".\Data\UserIDs.txt" -ErrorAction SilentlyContinue

#Remove all addins
RemoveAllAddins

#Add them back if the user is in the list of user names
if($UIDsInFile -contains $llusername){
    InstallAddin
}

Upvotes: 1

Related Questions