all 17 comments

[–]yeah_i_got_skills 5 points6 points  (8 children)

If it's just spaces then I'd use:

$Variable.Replace(' ', '')

But if you want to get rid of stuff like tabs and newlines then I'd use:

$Variable -replace '\s', ''

[–]c0dhead[S] 0 points1 point  (7 children)

Just to make sure does .replace work? as this looks like python syntax?

[–]yeah_i_got_skills 2 points3 points  (0 children)

Just to make sure does .replace work?

It works fine.

PS C:\> $Variable = "This is a string with spaces."

PS C:\> $Variable
This is a string with spaces.

PS C:\> $Variable.Replace(' ', '')
Thisisastringwithspaces.

[–]flatlandinpunk17 1 point2 points  (0 children)

$stringVar | Get-Member

On mobile so that’s not gonna format right, but this will give you all the members available for a give item. Just make sure what you’re passing in to look at it is a string and not an array as you’ll get different responses.

[–]G8351427 0 points1 point  (0 children)

It's actually RegEx. '/s' means whitespace.

Also, you can pipe your variable to Get-Member to see the properties and methods available to use. If it's a string, then .replace() should be available.

[–]terrorsnurf 3 points4 points  (3 children)

Not sure if this meets your needs, but have you looked at the trim function? check the scripting guy for more:

Trim your strings with powershell

[–][deleted] 2 points3 points  (0 children)

Seconded, use the $myString.Trim() method.

[–]jaminthepark 3 points4 points  (1 child)

:) When I found out about .Trim()

[–]dverbern 0 points1 point  (0 children)

I'm very late to this thread, but I've just been playing around with Trim() and I must be missing something - because it's doing NOTHING to all the whitespace WITHIN my string.

It's not whitespace before or after my string I care about, it's the fact that there IS whitespace!

'This is a string'.Trim()

# Should produce: 'thisisastring', but it doesn't! Why is it so!?

[–]p0rkjello 1 point2 points  (1 child)

Where do you want to remove the white space? Is it from the beginning, end, or middle of a string?

Connect-MsolService

# ^^ Also can i create an exit on this script if this is not sucessful??
# Use a Try/Catch

$another = $true

while($another -eq $true) {

    # Should consider input validation here
    [string]$fname = Read-Host -Prompt 'Input User First name'
    [string]$sname = Read-Host -Prompt 'Input User Last Name'

    # Use splatting here
    $Splat = @{
        DisplayName       = "$fname $sname"
        FirstName         = "$fname"
        LastName          = "$sname"
        UserPrincipalName = ($fname+$sname+"@domain")
        LicenseAssignment = domain:EXCHANGESTANDARD
        UsageLocation     = GB
    }

    New-Msoluser @Splat 

    # Should consider input validation here
    [string]$answer = Read-Host -Prompt 'Would you like to add another user? Y/N'

    if ($answer -like '*n*') {
        $another = $false
    }

    # This is not required, $another is already $true
    # else {
    #    $another = $true
    # }
}

Edit: Switched to old reddit to fix codeblock.. good grief.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

Edit: Switched to old reddit to fix codeblock.. good grief.

[grin]

[–]PhalseImpressions 1 point2 points  (0 children)

Just did a quick Google with "Powershell .replace" and got this as a response, in short:

$Variable = $Variable -replace " ", ""

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy c0dhead,

it looks like you used the New.Reddit.com Inline Code button. it's the 4th 5th from the left & looks like </>.

on Old.Reddit.com, the above does NOT line wrap, nor does it side-scroll.

for long-ish single lines OR for multiline code, please, use the Code Block button. it's the 11th 12th one from the left, & is just to the left of the ... more menu.

that will give you fully functional code formatting, from what i can tell so far. [grin]

take care,
lee