Get substring

Welcome Forums General PowerShell Q&A Get substring

Viewing 8 reply threads
  • Author
    Posts
    • #205899
      MvW
      Participant
      Topics: 10
      Replies: 13
      Points: 113
      Rank: Participant

      Can you help me?

      For example, how do I get “{90150000-012B-040C-0000-0000000FF1CE}” “{D17A4D8B-29D2-4432-A08E-F89F965FDA60}” from the string

      “C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe /removereleaseinpatch” “{90150000-012B-040C-0000-0000000FF1CE}” “{D17A4D8B-29D2-4432-A08E-F89F965FDA60}” “1043” “0” or

      “C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe” /removereleaseinpatch “{90150000-012B-040C-0000-0000000FF1CE}” “{D17A4D8B-29D2-4432-A08E-F89F965FDA60}” “1043” “0”

      etc.

    • #205902
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      What have you tried so far? Please show your code.

      The simplest approach would be something like this:

      $String = '"C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe /removereleaseinpatch" "{90150000-012B-040C-0000-0000000FF1CE}" "{D17A4D8B-29D2-4432-A08E-F89F965FDA60}" "1043" "0"'
      $String -match '"{.*}"'
      $Matches
    • #205926
      MvW
      Participant
      Topics: 10
      Replies: 13
      Points: 113
      Rank: Participant

      I tried with split -match etc.

      But the end result is

      msiexec /x “{90150000-012B-0413-0000-0000000FF1CE}” MSIPATCHREMOVE={D17A4D8B-29D2-4432-A08E-F89F965FDA60}/qn REBOOT=REALLYSUPPRESS

    • #205944
      MvW
      Participant
      Topics: 10
      Replies: 13
      Points: 113
      Rank: Participant

      I craete this code, but it goes wrong on /removereleaseinpatch

      (((Seach-InstalledSoftware -name “*Skype”).UninstallString))

      Output: “C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe” /removereleaseinpatch “{90150000-012B-040C-0000-0000000FF1CE}” “{D17A4D8B-29D2-4432-A08E-F89F965FDA60}” “1043” “0”

      $String -match ‘”{.*}”‘ > $null

      $A=($Matches[0]).split(” “)
      $b = “msiexec /x $($A[0]) MSIPATCHREMOVE=$($A[1]) /qn REBOOT=REALLYSUPPRESS”
      $B

    • #205947
      Participant
      Topics: 12
      Replies: 523
      Points: 1,214
      Helping Hand
      Rank: Community Hero
      $myInput = @(
      '"C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe" /removereleaseinpatch" "{90150000-012B-040C-0000-0000000FF1CE}" "{D17A4D8B-29D2-4432-A08E-F89F965FDA60}" "1043" "0"' 
      '"C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe" /removereleaseinpatch" "{12345678-012B-040C-0000-0000000FF1CE}" "{abcdefgh-29D2-4432-A08E-F89F965FDA60}" "1043" "0"'
      )
      
      $myOutput = foreach ($Item in $myInput) { 
          [PSCustomObject]@{
              Guid1 = $Item.Split('{')[1].Split('}')[0]
              Guid2 = $Item.Split('{')[2].Split('}')[0]
          }
      }
      
      $myOutput | FT -a 
      
      Guid1                                Guid2                               
      -----                                -----                               
      90150000-012B-040C-0000-0000000FF1CE D17A4D8B-29D2-4432-A08E-F89F965FDA60
      12345678-012B-040C-0000-0000000FF1CE abcdefgh-29D2-4432-A08E-F89F965FDA60
      

      I would point out here that the splitting in lines 8 and 9 is done via the braces {} and not the spaces or double-quotes

      • This reply was modified 3 months, 1 week ago by Sam Boutros.
      • This reply was modified 3 months, 1 week ago by Sam Boutros.
    • #205953
      MvW
      Participant
      Topics: 10
      Replies: 13
      Points: 113
      Rank: Participant

      Guy’s

      Greate but the out-put  from the function is:

      ((Seach-InstalledSoftware -name “*Skype”).UninstallString))

      Output: C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe /removereleaseinpatch {90150000-012B-040C-0000-0000000FF1CE} {D17A4D8B-29D2-4432-A08E-F89F965FDA60} 1043 0

       

      • This reply was modified 3 months, 1 week ago by MvW.
    • #205962
      Participant
      Topics: 12
      Replies: 523
      Points: 1,214
      Helping Hand
      Rank: Community Hero

      Guy’s

      Greate but the out-put is from an function

      ((Seach-InstalledSoftware -name “*Skype”).UninstallString))

      Output: C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\Oarpmany.exe /removereleaseinpatch {90150000-012B-040C-0000-0000000FF1CE} {D17A4D8B-29D2-4432-A08E-F89F965FDA60} 1043 0

      (((Seach-InstalledSoftware -name “*Skype”).UninstallString)) | foreach { 
          [PSCustomObject]@{
              Guid1 = $PSItem.Split('{')[1].Split('}')[0]
              Guid2 = $PSItem.Split('{')[2].Split('}')[0]
          }
      }
      
      • This reply was modified 3 months, 1 week ago by Sam Boutros.
      • This reply was modified 3 months, 1 week ago by Sam Boutros.
      • This reply was modified 3 months, 1 week ago by Sam Boutros.
    • #205974
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      OK. I assume you want to remove some installed software, right? There are several examples out there on the internet. You don’t have to re-invent the wheel. One good place to start your search will be the PowershellGallery.

    • #206643
      Participant
      Topics: 0
      Replies: 6
      Points: 84
      Rank: Member

      To answer your original question regarding getting the substring I think I have a solution for you. First I replaced your curly quotes with normal quotes $([char] 34) and saved the text to the file LongText.txt.  Here is the command to pull just the substrings.

       

          
      get-content .\longtext.txt | foreach-object {
          if ($_ -match '("{.+}" "{.+}")') {
              $matches[1]
          }
      }
      
      

      I hope this helps. Good luck in whatever you are doing.

Viewing 8 reply threads
  • The topic ‘Get substring’ is closed to new replies.