Deleting spaces in variable

Welcome Forums General PowerShell Q&A Deleting spaces in variable

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #6298

    Points: 0
    Rank: Member

    by BassieBas at 2013-03-04 01:14:54

    Hello everyone.

    I have made a powershell script to create a new client with multiple users in our environment.
    That is working just fine, but the only thing is that i need to delete te spaces in a variable.

    The name of the company (Micro Soft) is used for the OU, Group and directory name.
    The only thing is that i need that companyname (Micro Soft) to be without spaces (MicroSoft).
    How can i manage to do this? I work with Powershell ISE on a Windows Server 2008R2 environment.

    In forward many thanks.

    by mjolinor at 2013-03-04 03:26:12

    The simplest way to remove the spaces from a string is with the .replace() method:

    $var = 'Micro Soft'
    $var = $var.replace(' ','')


    by compugab at 2013-03-04 03:37:48

    You could also use a more "PowerShell Way" with -replace like this :

    $a = "a b c d"
    $a -replace " ", ""

    by BassieBas at 2013-03-04 03:42:24

    That worked, thank you so much for the fast reply!

The topic ‘Deleting spaces in variable’ is closed to new replies.