locked
How can I shorten this sting replacement & excluding? RRS feed

  • Question

  • $filepath="\\networklocation\testfile.txt"
    $badstring2='\.Junktext'
    (Get-Content $filepath) | Foreach-Object {
    $srch4quid="{(.*?)}"
    $result = [regex]::match($_, $srch4quid).groups[1].value
    $badstring1 = "{$result}"
    (Get-Content $filepath) |Foreach-Object {$_ -replace  $badstring1, "" } |Set-Content $filepath
    (Get-Content $filepath) |foreach-Object {$_ -replace  $badstring2, "" }| Set-Content $filepath
    }
    (Get-Content $filepath) | Where-Object {$_ -cnotmatch 'Junktext2.*$'} | Set-Content $filepath
    (Get-Content $filepath) | Where-Object {$_ -Cnotmatch 'Junktext3.*$'} | Set-Content $filepath


    Tuesday, September 4, 2018 3:53 PM

Answers

  • What is it that you are trying to export?  There are commands that will get printer information without requiring string manipulation.

    Hint: When using replace to remove a match don't use an empty string.

    $string -replace 'some match'

    To remove a GUID:

    'GUID HERE {sdsdfsdfsdfd} END GUID' -replace '{.*}'


    \_(ツ)_/

    • Proposed as answer by I.T Delinquent Wednesday, September 5, 2018 10:09 AM
    • Marked as answer by WishIknewmorePS Friday, September 7, 2018 3:51 PM
    Wednesday, September 5, 2018 3:12 AM
  • To replace multiple items:

    'one xxx two xxxx three' -replace 'one|two|three'


    \_(ツ)_/

    Wednesday, September 5, 2018 3:13 AM

All replies

  • what has this got to do with "Windows 10 Networking"?
    Tuesday, September 4, 2018 6:10 PM
  • Sorry I'm new to this forum.  Wasn't sure where to place this.

    I've exported HKCU\printers.  The script will then find and remove each of the GUIDs.  Then it will remove a couple of other lines so it can be imported into another system.  

    Tuesday, September 4, 2018 6:59 PM
  • What is it that you are trying to export?  There are commands that will get printer information without requiring string manipulation.

    Hint: When using replace to remove a match don't use an empty string.

    $string -replace 'some match'

    To remove a GUID:

    'GUID HERE {sdsdfsdfsdfd} END GUID' -replace '{.*}'


    \_(ツ)_/

    • Proposed as answer by I.T Delinquent Wednesday, September 5, 2018 10:09 AM
    • Marked as answer by WishIknewmorePS Friday, September 7, 2018 3:51 PM
    Wednesday, September 5, 2018 3:12 AM
  • To replace multiple items:

    'one xxx two xxxx three' -replace 'one|two|three'


    \_(ツ)_/

    Wednesday, September 5, 2018 3:13 AM