2

The PowerShell command

Get-ADFSRelyingPartyTrust | select Name | out-file C:\listOfNames.txt

generates a file as follows:

Name
----
AustriaRP
BahamasRP
BrazilRP
CanadaRP

[...]

Now, how can I check if BrazilRP has been extracted and C:\listOfNames.txt contains it?

1
  • (gc C:\listOfNames.txt) -contains 'BrazilRP' Commented Feb 27, 2015 at 15:21

3 Answers 3

9

The Get-Content and then Select-String should help. If the string is in the file it will get returned. If not then the command will returned empty value.

Get-Content C:\listOfNames.txt | Select-String "BrazilRP"

If the "BrazilRP" occurs more than once all the occurrences will returned so you know if you got any duplicates. Same holds if the string is a part of a longer expression. For example if you search for "zil" then "BrazilRP" will be returned as well.

Also you can pipe the results out to another file:

Get-Content C:\listOfNames.txt | Select-String "BrazilRP" | Out-File C:\myResults.txt
Sign up to request clarification or add additional context in comments.

Comments

9

I found a solution (but thanks to PiotrWolkowski to suggest me the Get-Content function):

$file = Get-Content "C:\listOfNames.txt"
$containsWord = $file | %{$_ -match "BrazilRP"}
if ($containsWord -contains $true) {
    Write-Host "There is!"
} else {
    Write-Host "There ins't!"
}

Comments

-1

If you want to easily see if a file contains your text try this

The [bool] type returns the data as either true or false instead of returning the actual data your searching for

if ([bool]((Get-Content -Path "C:\listOfNames.txt") -like '*BrazilRP*')) { 
write-host "found it"
}
else {
 write-host "didnt find it"
}

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.