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 6158339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:56:28+00:00 2026-05-23T20:56:28+00:00

I am using a PowerShell script to find all occurrences of a regex expression

  • 0

I am using a PowerShell script to find all occurrences of a regex expression and output it to file. I have two objectives for this question.

  1. Remove leading white space from a column’s value
  2. Specify a width for an extra field (LineNumbers)

This is what I have so far:

gci -recurse -include *.* | Select-String -pattern $regexPattern |`
Format-Table -GroupBy Name -Property Path, Line -AutoSize

This outputs the following:

Path                      Line                                                   
----                      ----
C:\myRandomFile.txt       This is line 1 and it is random text.
C:\myRandomFile.txt                This is line 2 and it has leading white space.
C:\myNextRandomFile.txt            This is line 3 and it has leading white space.
C:\myLastRandomFile.txt                         This is line 4. 

This is because the files have leading white spaces (actually indents/tab spaces, but outputted as white spaces). I can’t change the original files and remove the leading white space as they are our production files/SQL scripts.

I want to trim the leading white space for the Line column so that the output looks like this:

Path                      Line                                                   
----                      ----
C:\myRandomFile.txt       This is line 1 and it is random text.
C:\myRandomFile.txt       This is line 2 and it has no leading white space.
C:\myNextRandomFile.txt   This is line 3 and it has no leading white space.
C:\myLastRandomFile.txt   This is line 4 and this is how it should look. 

And, if I add the LineNumbers column by using

-property LineNumbers 

then the LineNumbers column take up about half the space in the row. Can I specify the width of the LineNumbers? I’ve tried the -AutoSize flag, but this doesn’t seem to work well. I’ve tried

LineNumber;width=50
LineNumber width=50
LineNumber -width 50

and all variations of this, but I get errors to the likes of “Format-Table: A parameter cannot be found that matches parameter name width=50”

  • 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-05-23T20:56:29+00:00Added an answer on May 23, 2026 at 8:56 pm

    I can’t test it right now, but I think this should do the trick or at least get you going in the right direction:

    gci -recurse -include *.* | Select-String -pattern $regexPattern |`
    Format-Table Path, @{Name='Line'; Expression={$_.Line -replace '^\s+', ''}; Width=50}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that I set using PowerShell that contains the version number
I have a powershell script below which takes a config file and deletes files
I have to invoke a PowerShell script from a batch file. One of the
[I have posted this question earlier in the PowerShell Technet forum, but without a
I am using switch parameters in my PowerShell script in this fashion. param( [switch]
How do I create an Application Pool on IIS 6.0 using a PowerShell script?
I'm using PowersHell to automate iTunes but find the error handling / waiting for
I'm trying to change a site's home directory using powershell. This is what I
I'm trying to modify the contents of an MSI file using Powershell. To do
I'm using the PowerShell script below to set anonymous user identity and authentication methods

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.