• A little PowerShell Gotchya!

    Home » Forums » Developers, developers, developers » DevOps Lounge » A little PowerShell Gotchya!

    Author
    Topic
    #1773818

    Hey Y’all,

    Here’s a little anomaly, or lack of understanding on my part, in the way the Trim function works in PowerShell.

    Code:
    #Setup:
    PS> $x = 'MicrosoftOfficeOffice Automatic Updates'
    PS> $p = 'MicrosoftOffice'
    
    #Test Trim Start
    PS> $x.TrimStart("$p")
     [COLOR=#0000ff]Automatic Updates
    [/COLOR]
    #Note how the TrimStart command not only trims the value of $P out of $X but also kills the 'Office' from 'Office Automatic Updates'!
    
    #It gets even worse if you use just Trim:
    PS> $x.Trim("$p")
     [COLOR=#0000ff]Automatic Upda[/COLOR]
    
    #Conclusion the Trim functions will get rid of any part of the specified value on the beginning or end of the trimmed string up to the first non recognized character.
    
    #To actually get what I was after I had to do this:
    
    PS> $x.Substring($p.Length,$x.Length-$p.length)
    [COLOR=#0000ff]Office Automatic Updates[/COLOR]
    
    
    

    Thus, Test, Test, and Test again!

    HTH :cheers:

    Reply To: A little PowerShell Gotchya!

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

    Your information: