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

  • Home
  • SEARCH
  • 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 8087751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:52:51+00:00 2026-06-05T18:52:51+00:00

Alright, this is driving me nuts because my regex is working on Rubular, but

  • 0

Alright, this is driving me nuts because my regex is working on Rubular, but PowerShell is not working as I expect.

  1. I did a Get-ChildItem on a network directory and then directed the output into a txt file.
  2. I went to remove the directory info from the text file that appears like the following:

enter image description here

  1. When I use PowerShell to try and write a regex to remove the Directory info, I run into some problems.

When I use:

$var = Get-Contnet "file path"
$var -match "Directory.*"

PowerShell grabs the text I am looking for, BUT it doesn’t grab the text that starts on a new line, I get:

Directory: \\Drive\Unit\Proposals\Names\Location\crazy folder path\even crazier folder path\unbelievable folder path\

So… when I use:

$var -match "Directory.*\n.*"

I get nothing…

When I try this on Rublar it works fine, what am I missing here? Any help would be great, 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-05T18:52:53+00:00Added an answer on June 5, 2026 at 6:52 pm

    Filburt’s answer is a good one, and it doesn’t look like regular expressions are the best tool to use here. However, you bumped into an issue that may cause confusion again down the road. The issue here is that the variable you populated with Get-Content is not a multi-line string. It is an array of strings:

    $var = Get-Content "file path"
    $var.GetType() # Shows 'Object[]'
    

    When you run a regex match against $var, it matches against each object in the array (each line in the file) individually. It can’t match past the end of a line because the next line is a new object.

    One workaround here is to flatten that array of strings down into a single string like this:

    $var = (Get-Content "file path" | Out-String)
    $var.GetType() # Shows 'String' now
    

    In Powershell it can sometimes be tricky to tell when you’re dealing with a single String object versus an array of Strings. If you output them to the console they appear identical. In those cases, GetType() and Out-String can be useful tools.

    Edit: As of Powershell 3.0, the Filesystem provider includes a -Raw switch for Get-Content. That switch instructs Get-Content to read the file all at once without splitting it into chunks. It is significantly quicker than using the Out-String workaround, because it doesn’t waste time pulling pieces apart only to put them back together again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright I know this is more than likely a amateur question but I have
Alright I don't see why this isnt working. It seems pretty simple. Here is
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, this may take a moment or two to explain: I'm working on creating
I have a boxplot with a dot representing the mean: This is alright, but
Alright I was looking at this in python but I just like ruby better.
Alright so i have been working on this Dynamic load of a spinner from
Alright - this may be a dumb question, but my desire for perfection is
Alright this is my first day with JQuery so have fun with these functions
Alright so this problem has been breaking my brain all day today. The Problem:

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.