0

I'm trying to extract the key for a user but I get unwanted spaces and newlines before the string and after it. My script is the following:

$File = gwmi Win32_UserProfile -co MADS000001 |
        select localpath, sid |
        Where-Object {$_.localpath -eq "C:\Users\Administrator"} |
        select Sid |
        ft -HideTableHeaders |
        Out-String -Stream
Write-Host $file

How can I get rid of them?

The output looks like this:

1
  • It's not clear to me what your goal/purpose is. Are you wanting to remove stale/obsolete user profiles? (Tell what you want to do, not how you think it needs to be done.) Commented Sep 5, 2018 at 14:45

3 Answers 3

1

I'm not sure of your goal.

If you want to find out the name of the local Administrator account (even if it's been renamed), you can write this:

Get-WmiObject Win32_UserAccount -Filter 'LocalAccount=TRUE AND SID LIKE "%-500"' |
  Select-Object -ExpandProperty Name

If you want that user's profile path, you can combine them:

$adminSID = Get-WmiObject Win32_UserAccount -Filter 'LocalAccount=TRUE AND SID LIKE "%-500"' |
  Select-Object -ExpandProperty SID
$profilePath = [WMI] "root\cimv2:Win32_UserProfile.SID='$adminSID'" |
  Select-Object -ExpandProperty LocalPath

Note the use of Select-Object -ExpandProperty to select a specific property and output only that property.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank you for your answer, not quite my goal, what i want it's find the SID for a user that will be a variable when i get this right, and then delete it. It's for recreating local profiles, deleting the registry entry. So i thought, finding the SID with the profilepath, and then deleting it with the full key path and then the SID that i got with that command. The problem is that i get those annoying spaces before and after the SID...
@ΑλέξανδροςΓιοςτουΉρας - See my other answer.
0

I think you are over complicating it a bit.
If you just do

$profileInfo = Get-WmiObject Win32_UserProfile -ComputerName 'MADS000001' |
               Where-Object {$_.localpath -like "*\Administrator"} |
               Select-Object LocalPath, Sid, PSComputerName

You will get an object (if found of course, $null otherwise) with the three properties in the Select-Object command.

The $profileInfo.LocalPath you can use to delete the folder.
The $profileInfo.Sid string value you can use to remove the registry key for that user at
HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\<SID>

P.s. The second 'unwanted' whitespace you have outlined is simply a newline Write-Host always adds unless you use Write-Host $profileInfo.Sid -NoNewline

Comments

0

To get the SID for a user:

$userName = "Administrator"
$sid = Get-WmiObject Win32_UserAccount -Filter "LocalAccount=TRUE AND Name='$username'" |
 Select-Object -ExpandProperty SID

Note that this will fail if the user is not named Administrator.

Also - note the use of Select-Object -ExpandProperty to select the value of a specific property of an object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.