• PowerShell Replace Code

    Home » Forums » Developers, developers, developers » DevOps Lounge » PowerShell Replace Code

    Author
    Topic
    #477724

    I’m working my way through the “Windows PowerShell in Action” book and I was thinking I understood it but that was a stretch.

    Here’s the code:

    Code:
    foreach-object ($f in dir *.xml) {$f = $f -replace ‘Bruces-PC’,’Inspiron15-i5′}

    Now as I understand this, obviously wrong, according to page 144 of the book, this should find all the .xml files in the current directory then replace all instances of Bruces-PC with Inspiron15-i5 and then write it back to the same filename!

    I’ve also tried the variant of {${$f} = ${$f} -replace …} and still no love.
    I’ve also tried it with a full path on the dir command w/o luck.

    Ideas? :cheers:

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!
    Computer Specs

    Viewing 5 reply threads
    Author
    Replies
    • #1287558

      Have you tried quotation marks as the string delimiter?

      Joe

      --Joe

    • #1287866

      My guess is that you haven’t specified the dir command properly. Try this: ($f in ‘dir *.xml’)

      cheers, Paul

    • #1287929

      Also, you cannot use the ${..} syntax with a variable. That is, ${$f} won’t work. Instead use “get-content $f”. Similaryl, use set-content to write to a file:

      [INDENT] foreach ($f in dir *.xml) {(get-content $f) -replace ‘Bruces-PC’,’Inspiron15-15′ | set-content $f}
      [/INDENT]

      BTW, are you looking at the first or second edition of PowerShell in Action? I assume first edition based on the page reference.

    • #1288044

      Cafed00d,

      You da’ MAN!:cool:

      My book is copyrighted 2007 and makes no mention of being a 2nd ed., so I assume it is the 1st ed.

      Thanks so much I now have a better handle on thing especially, the not using a variable in the ${} construct . I’ve still got a ways to go to get through the book.

      Thanks again.:cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1288568

      The second edition just came out – it’s almost twice as thick as the first! Can’t wait to work my way through it.

      I do have a suggestion: never try a long command unless you have first tried the parts. For example, I found out the ${$f} doesn’t work, while ${some.xml} does by at the command line assigning $f to a file, and then entering just ${$f} in the command line. I also played with an empty foreach command to make sure that it found the files I was looking for:

      foreach ($f in dir *.xml) {$f}

      And then I tried various individual command to see what it would take to read a file:

      get-content $f

      Only once I was sure the individual pieces worked did I string the full command together.

      All the best in your PowerShell studies!

    • #1288569

      Cafed00d,

      I was doing exactly as you stated but had not got all the way there yet. I also did not yet know about the get-content cmdlet.

      Thanks Again. :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 5 reply threads
    Reply To: PowerShell Replace Code

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: