1

I'm trying to create a report by cross referencing two text documents. I have C:\formeremployees.txt and C:\shareaudit.txt. As you can guess the formeremployees.txt has a list of former employee usernames only. No Headers; only usernames. The C:\shareaudit.txt contains a list of every folder on a share with the ACL info on the same line next to the folder path.

Here was my attempt at creating a report that only lists the lines that have user accounts from the formeremployees.txt:

$Users = Get-Content C:\formeremployees.txt

foreach ($User in $Users) {
  $Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "$User"
  $Output.Line | Out-File C:\completereport.txt -Append
}

But unfortunately, I get the following error:

Select-String : Cannot bind argument to parameter 'Pattern' because it is an
empty string.
At line:7 char:71
+ $Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "$User"
+                                                            ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Select-String], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SelectStringCommand

Select-String : Cannot bind argument to parameter 'Pattern' because it is an
empty string.
At line:7 char:71
+ $Output = Select-String -Path "C:\ShareAudit.txt" -Pattern "$User"
+                                                            ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Select-String], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SelectStringCommand

Then I get a sad, empty completereport.txt file. I can't seem to get this to work or know if it's possible.

Edit________________________

Here's what else I've tried and the results:

$Users = Get-Content C:\formeremployees.txt|?{!([string]::IsNullOrWhitespace($_))}

foreach ($User in $Users) {
  $Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "$User"
  $Output.Line | Out-File C:\completereport.txt -Append
}

This gave me a blank C:\completereport.txt document.

$Users = Get-Content C:\formeremployees.txt|?{!([string]::IsNullOrWhitespace($_))}
$Pattern = ($Users|ForEach{[regex]::escape($_)}) -join '|'
Get-Content "C:\Shareaudit.txt" | Where{$_ -match $Pattern} | Set-Content C:\completereport.txt 

This as far as I can tell didn't do anything. There was no completereport.txt document created when it finished.

$Users=(Get-Content C:\formeremployees.txt) -ne ''

foreach ($User in $Users) {
  $Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "$User"
  $Output.Line | Out-File C:\completereport.txt -Append
}

This gave me a blank text document.

 $Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "<single username from formeremployeee.txt>"
  $Output.Line | Out-File C:\completereport.txt -Append

When I put in a username that I knew still had permissions to some folders in the share and was also in the formeremployee.txt, the script worked as intended and gave me a list of the folders I needed so there's nothing wrong with the bottom part of the script, so I'm guessing something is up with the formeremployee.txt or the way I used the $Users variable.

To test further, I tried this:

$Users=(Get-Content C:\formeremployees.txt) -ne ''

foreach ($User in $Users) {
  Select-String -Path "C:\Shareaudit.txt" -Pattern "$User"
}

This didn't output any results. The text formeremployee.txt file lists the usernames as follows:

username1
username2
username3
username4

Is it in the wrong format for this?

4
  • 2
    Check if there actually are empty elements: foreach ($User in $Users) { "-$User-" } Commented Sep 1, 2016 at 21:58
  • This listed the users as: -username - -username1 - -username2 - Commented Sep 2, 2016 at 17:55
  • Those elements would not produce the error you said you're getting. Please provide sample input data that would allow us to reproduce the issue. Commented Sep 4, 2016 at 19:20
  • Same error and none of these have worked for me. I'm using a single regex, and it outputs from the variable. However, if I try it as a pattern, I get this error "because it is an empty string". And get this, if I try to $regexvar | gm, I get an error that "You must specify an object for the Get-Member cmdlet." Has anyone actually solved the problem of why an assigned var looks like an empty string in 'pattern'? Commented Jan 30, 2024 at 18:30

1 Answer 1

4

The most obvious answer is that you have blank lines in your FormerEmployee.txt file. The simplest solution is to update your first line:

$Users = Get-Content C:\formeremployees.txt|?{!([string]::IsNullOrWhitespace($_))}

What I would probably do to speed things up is make a regex pattern out of the users, and run the Select-String once, instead of once per user:

$Users = Get-Content C:\formeremployees.txt|?{!([string]::IsNullOrWhitespace($_))}
$Pattern = ($Users|ForEach{[regex]::escape($_)}) -join '|'
Get-Content "C:\Shareaudit.txt" | Where{$_ -match $Pattern} | Set-Content C:\completereport.txt
Sign up to request clarification or add additional context in comments.

5 Comments

Why not (Get-Content C:\formeremployees.txt) -ne ''?
Will 'Set-Content C:\completereport.txt' create the file if it doesn't exist? I ran this to see if it worked. It's been running for about 2 minutes now, and there's no C:\completereport.txt file yet, but I'll wait until the script has finished. Thanks for the help though. I'm still learning and this gives me a lot to look into.
While I didn't get any errors using your script from above, a C:\completereport.txt file was not created with the results.
without sample data to replicate the issue it is difficult to troubleshoot any issues you are having.
I had spaces after each username in the formeremployee.txt file. Just replaced space with nothing and I got the report I needed. You were right in the first place and I wanted to give credit where credit is due. Rookie mistake on my end.

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.