Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Listing out all usernames with space

Reply
 
Old 08-01-2007   #1 (permalink)
Kari


 
 

Listing out all usernames with space

Hi!

It seems that we have a bunch of users whose sAMAccountName and
userPrincipalName fields contain space characters. This is a mistake that was
caused by some serious cut 'n paste work done by our user account management.

So I need to list all the users whose sAMAccountName contains the space
character and then fix them. For example the value could be:
"abc1234 " <- so this is the case mostly, the space is located in the end of
the sAMAccountName
or
"abc 1234" <- it could be in the middle
or
" abc1234" <- it could be in the start

Does query:
GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 | Where-Object {
$_.LogonName -like "* *" }
.... return all the cases listed above?

If I now have them in the pipeline how can I remove all the spaces from the
sAMAccount -field (so no parsing on Name -field and so on!)?

My System SpecsSystem Spec
Old 08-01-2007   #2 (permalink)
Marco Shaw


 
 

Re: Listing out all usernames with space

Kari wrote:
> Hi!
>
> It seems that we have a bunch of users whose sAMAccountName and
> userPrincipalName fields contain space characters. This is a mistake that was
> caused by some serious cut 'n paste work done by our user account management.
>
> So I need to list all the users whose sAMAccountName contains the space
> character and then fix them. For example the value could be:
> "abc1234 " <- so this is the case mostly, the space is located in the end of
> the sAMAccountName
> or
> "abc 1234" <- it could be in the middle
> or
> " abc1234" <- it could be in the start
>
> Does query:
> GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 | Where-Object {
> $_.LogonName -like "* *" }
> ... return all the cases listed above?
>
> If I now have them in the pipeline how can I remove all the spaces from the
> sAMAccount -field (so no parsing on Name -field and so on!)?


Although I was surprised it was that easy, it does appear that "* *"
will work...

I thought you'd need a more complex regular expression.

PS C:\> "test" -like "*"
True
PS C:\> "test" -like "* "
False
PS C:\> "test ing" -like "* "
False
PS C:\> "test ing" -like "* *"
True
PS C:\> " testing" -like "* *"
True
PS C:\> "testing " -like "* *"
True
PS C:\>
My System SpecsSystem Spec
Old 08-01-2007   #3 (permalink)
dreeschkind


 
 

RE: Listing out all usernames with space

"Kari" wrote:

> Hi!
>
> It seems that we have a bunch of users whose sAMAccountName and
> userPrincipalName fields contain space characters. This is a mistake that was
> caused by some serious cut 'n paste work done by our user account management.
>
> So I need to list all the users whose sAMAccountName contains the space
> character and then fix them. For example the value could be:
> "abc1234 " <- so this is the case mostly, the space is located in the end of
> the sAMAccountName
> or
> "abc 1234" <- it could be in the middle
> or
> " abc1234" <- it could be in the start
>
> Does query:
> GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 | Where-Object {
> $_.LogonName -like "* *" }
> ... return all the cases listed above?


Yep, I think this should work. Another way would be:

PS> ' brat wurst banane '.ToCharArray() -contains ' '
True

> If I now have them in the pipeline how can I remove all the spaces from the
> sAMAccount -field (so no parsing on Name -field and so on!)?


PS> $newname = ' brat wurst banane ' -replace ' ',''
PS> $newname
bratwurstbanane

--
greetings
dreeschkind
My System SpecsSystem Spec
Old 08-01-2007   #4 (permalink)
Hecks


 
 

Re: Listing out all usernames with space


"dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
news:7D957437-8F74-479F-9297-B5DB218FFADF@microsoft.com...
> "Kari" wrote:
>
>> Hi!
>>
>> It seems that we have a bunch of users whose sAMAccountName and
>> userPrincipalName fields contain space characters. This is a mistake that
>> was
>> caused by some serious cut 'n paste work done by our user account
>> management.
>>
>> So I need to list all the users whose sAMAccountName contains the space
>> character and then fix them. For example the value could be:
>> "abc1234 " <- so this is the case mostly, the space is located in the end
>> of
>> the sAMAccountName
>> or
>> "abc 1234" <- it could be in the middle
>> or
>> " abc1234" <- it could be in the start
>>
>> Does query:
>> GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 | Where-Object {
>> $_.LogonName -like "* *" }
>> ... return all the cases listed above?

>
> Yep, I think this should work. Another way would be:
>
> PS> ' brat wurst banane '.ToCharArray() -contains ' '
> True
>
>> If I now have them in the pipeline how can I remove all the spaces from
>> the
>> sAMAccount -field (so no parsing on Name -field and so on!)?

>
> PS> $newname = ' brat wurst banane ' -replace ' ',''
> PS> $newname
> bratwurstbanane
>
> --
> greetings
> dreeschkind


Or simply:

PS> $newname = ' brat wurst banane ' -replace '\s'
PS> $newname
bratwurstbanane

-Hecks


My System SpecsSystem Spec
Old 08-01-2007   #5 (permalink)
Marco Shaw


 
 

Re: Listing out all usernames with space

Marco Shaw wrote:
> Kari wrote:
>> Hi!
>>
>> It seems that we have a bunch of users whose sAMAccountName and
>> userPrincipalName fields contain space characters. This is a mistake
>> that was caused by some serious cut 'n paste work done by our user
>> account management.
>>
>> So I need to list all the users whose sAMAccountName contains the
>> space character and then fix them. For example the value could be:
>> "abc1234 " <- so this is the case mostly, the space is located in the
>> end of the sAMAccountName
>> or
>> "abc 1234" <- it could be in the middle
>> or
>> " abc1234" <- it could be in the start
>>
>> Does query:
>> GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 | Where-Object
>> { $_.LogonName -like "* *" }
>> ... return all the cases listed above?
>>
>> If I now have them in the pipeline how can I remove all the spaces
>> from the sAMAccount -field (so no parsing on Name -field and so on!)?


Sorry, I skipped over the part where you also wanted to *fix* the
spaces... Others have answered by now...
My System SpecsSystem Spec
Old 08-02-2007   #6 (permalink)
Kari


 
 

Re: Listing out all usernames with space

> > PS> $newname = ' brat wurst banane ' -replace ' ',''
> > PS> $newname
> > bratwurstbanane
> >
> > --
> > greetings
> > dreeschkind

>
> Or simply:
>
> PS> $newname = ' brat wurst banane ' -replace '\s'
> PS> $newname
> bratwurstbanane
>
> -Hecks



Ok. But how to put this in action?

Let's say that I query the users first to the pipeline or save them like this:
$spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
Where-Object { $_.LogonName -like "* *" } | select-object logonname

How should I run the replace on this array? I tried with one test user (one
user object stored in the $spaceusers:
$spaceusers -replace '\s'

.... and it just returns "@{LogonName=xyz8976} but doesn't actually modify
anything.
My System SpecsSystem Spec
Old 08-02-2007   #7 (permalink)
Kari


 
 

Re: Listing out all usernames with space

> Ok. But how to put this in action?
>
> Let's say that I query the users first to the pipeline or save them like this:
> $spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
> Where-Object { $_.LogonName -like "* *" } | select-object logonname
>
> How should I run the replace on this array? I tried with one test user (one
> user object stored in the $spaceusers:
> $spaceusers -replace '\s'
>
> ... and it just returns "@{LogonName=xyz8976} but doesn't actually modify
> anything.


So this probably does the trick for the array, but how can I commit the
changes from the modified array back to LDAP? Is it wise to handle these as
objects all the way?
My System SpecsSystem Spec
Old 08-02-2007   #8 (permalink)
Hecks


 
 

Re: Listing out all usernames with space


"Kari" <Kari@discussions.microsoft.com> wrote in message
news63D867E-8CA2-4F6A-B9A8-CF95360C13E5@microsoft.com...
>> Ok. But how to put this in action?
>>
>> Let's say that I query the users first to the pipeline or save them like
>> this:
>> $spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
>> Where-Object { $_.LogonName -like "* *" } | select-object logonname
>>
>> How should I run the replace on this array? I tried with one test user
>> (one
>> user object stored in the $spaceusers:
>> $spaceusers -replace '\s'
>>
>> ... and it just returns "@{LogonName=xyz8976} but doesn't actually modify
>> anything.

>
> So this probably does the trick for the array, but how can I commit the
> changes from the modified array back to LDAP? Is it wise to handle these
> as
> objects all the way?


Try adding the replace action to the pipeline, like so:

$spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
Where-Object { $_.LogonName -like "* *" } | select-object logonname |
Foreach-Object {$_.logonname -replace "\s"}

which is equivalent to::

$spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
Where-Object { $_.LogonName -like "* *" } | select-object logonname

$spaceusers = $spaceusers | % {$_.logonname -replace "\s"}

You could probably get away with leaving out the Where-Object part of the
pipeline, depending on the object emitted by GeQADUser. Maybe:

$spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
Foreach-Object { $_.LogonName -replace "\s" }

Sorry, I can't help you with the second question!

-Hecks


My System SpecsSystem Spec
Old 08-02-2007   #9 (permalink)
Kari


 
 

Re: Listing out all usernames with space

> Try adding the replace action to the pipeline, like so:
>
> $spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
> Where-Object { $_.LogonName -like "* *" } | select-object logonname |
> Foreach-Object {$_.logonname -replace "\s"}
>
> which is equivalent to::
>
> $spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
> Where-Object { $_.LogonName -like "* *" } | select-object logonname
>
> $spaceusers = $spaceusers | % {$_.logonname -replace "\s"}
>
> You could probably get away with leaving out the Where-Object part of the
> pipeline, depending on the object emitted by GeQADUser. Maybe:
>
> $spaceusers = GetQADUser -SearchRoot "mydomain.local/ou" -SizeLimit 0 |
> Foreach-Object { $_.LogonName -replace "\s" }
>
> Sorry, I can't help you with the second question!
>
> -Hecks
>


Let's say that I have an user named "Mickey Mouse" whose username is
"mickey1 ". Now if I want to query for Mickey Mouse and change his username
to "mickey1" it would be done like this:

GetQADUser -name 'Mickey Mouse' -SizeLimit 0 |select-object logonname |
Foreach-Object {$_.logonname -replace "\s"}

So now I finally have an answer. Thank you!
My System SpecsSystem Spec
Old 08-02-2007   #10 (permalink)
Kari


 
 

Re: Listing out all usernames with space


"Kari" wrote:
> Let's say that I have an user named "Mickey Mouse" whose username is
> "mickey1 ". Now if I want to query for Mickey Mouse and change his username
> to "mickey1" it would be done like this:
>
> GetQADUser -name 'Mickey Mouse' -SizeLimit 0 |select-object logonname |
> Foreach-Object {$_.logonname -replace "\s"}
>
> So now I finally have an answer. Thank you!


.... or that's what I thought. That doesn't do anything. If I change Mickeys
username to "mickey 1" and run the command above nothing happens except it
prints out the username in correct form "mickey1". So how can I apply my
changes to LDAP?

(it sucks that you can't modify your posts!)
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
stored usernames and passwords Vista General
How can I hide usernames from Welcome Screen? Vista General
Home folders not displaying usernames Vista General
passwords and usernames Vista account administration
No numeric usernames? Vista account administration


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46