Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:16:23+00:00 2026-06-12T07:16:23+00:00

I have a simple question regarding code blocks within statements, and variables. My script

  • 0

I have a simple question regarding code blocks within statements, and variables. My script gathers user input and creates a string that can be used in a powershell statement later on.

$filter = @()
While ($filter[-1] -ne "end") {
  $filter += read-host 'Type the name of an OU you want to filter or type "end" to start the script'
  if ($filter[-1] -eq "end") {
    break
  }
  $strFilter += "`$_`.DistinguishedName -notlike `"*" + $filter[-1] + "*`" "
  $strFilter += "-and "
}
$strFilter = $strFilter.SubString(0,$strFilter.length-5)

Basically the user will type in however many words, and those words will be appended to the string "$_.DistinguishedName -notlike "*<WORD>*"" and throw them together with -and until the last word entered.

That part works fine, it’s using this generated string to gather information that doesn’t seem to work; I have a feeling it’s something very simple that I’m just missing.

For example:

$computers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem","CanonicalName","Description"
$filteredComputers = $computers | where-object { $strFilter }

$filteredComputers returns the same results as $computers which leads me to believe that the Where-object statement with my string variable isn’t doing anything.

Appreciate any help, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:16:24+00:00Added an answer on June 12, 2026 at 7:16 am

    The Where-Object scriptblock should evaluate to a Boolean value (or something that can be coerced to a Boolean value). You’re providing just a string which, if not empty, PowerShell will coerce to a Boolean $true. Try something like this instead:

    $strFilter = @()
    while (...) { ...
        $strFilter += "*$($filter[-1])*"
    }
    
    $filteredComputers = $computers | 
       Where {$dn=$_.DistinguishedName; @($strFilter | Where {$dn -like $_}).Count -eq 0}
    

    This uses a nested pipeline to walk each filter term and apply it to the current object’s DistinguishedName – which we had to stash because in the nested pipeline $_ takes on the value of the current $strFilter array element. If any of those pass the like test then the count will be greater than zero and won’t get passed down the pipeline. Only those that don’t match any like (count == 0) should get passed on down the pipeline.

    Here’s an example of this approach:

    C:\PS> $strFilter = '*ba*','*bog*'
    C:\PS> 'foo','bar','baz','abba','boggle' | 
               Where {$dn=$_; @($strFilter | Where {$dn -like $_}).Count -eq 0}
    
    foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question regarding accessing member variables of a model object. I
I have simple question. User supplies URL to my PHP script where I fetch
I have a simple question regarding jQuery selectors. I'm using the tablesorter and I
I have simple type Question : public class Question { public string[] Tags {
I have a simple question: how exactly are array variables such as $_SESSION and
I have a simple question regarding Entity declaration in JPA. I have an entity
Simple question regarding data persistance between application sessions. My application allows the user to
I have a simple question regarding the Minimax algorithm: for example for the tic-tac-toe
I have a question regarding the Java Memory Model. Here is a simple class
I have a simple question regarding PHP to check if this is the last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.