Tag Archives: PowerShell Programming language

PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Platforms are Windows PowerShell .NET Framework, Implementation language on C#.

Get User SID in Command Prompt or PowerShell

How to Get Windows User SID

For system administrators, querying information about the environment and configuration is an everyday job. One of these information is the Security Identifier (SID), which is used by Windows to identify users and groups. User names on a network can be duplicated, so that there is no conflict, each user is assigned a unique SID.

When assowing user rights, Windows uses the SID defined for that purpose. A SID is roughly similar to a Globally Unique Identifier (GUID) that each object in Windows owns. However, SIDs receive only security-relevate objects, because the SID is used for authentication of authenticity.

This SID identifies the user across the network. Even if the user’s name is changed, the SID persists, the user on the network is deleted and his SID is unchanged.

Structure of my SID

S-1-5-214147432549-3588766049-1627529166-1001

The SID (Security Identifier) tokens have the following meanings:

SIt is a SID
1Revision
5Identifier Authority
18System profiles
19Localservice
20Networkservice
21User profile
4147432549-3588766049-1627529166Domain ID, Computer ID
1001User ID (RID)

Table with SID of system accounts

Query SID of all user accounts

If you want to get the SID of all user accounts. You can do so with the following command in a Command Prompt Win+Rcmd

wmic useraccount get sid,name

All SIDs and user names are output.

C:\>wmic useraccount get sid,name
Name SID
Administrator
S-1-5-21-4147432549-3588766049-1627529166-500
DefaultAccount
S-1-5-21-4147432549-3588766049-1627529166-503
John
S-1-5-21-4147432549-3588766049-1627529166-1001
Guest
S-1-5-21-4147432549-3588766049-1627529166-501

Here are the SIDs of the local accounts. For a query in a network domain, there may be some more.

Computer and domain SIDs consist of a base SID and a relative ID (RID) appended to the base SID. If the computer belongs to a domain, another SID comes into play. The computer still has its own computer SID and local accounts and groups. But is also a member of a domain and therefore has a SID that represents the computer account in that domain. The SID of a computer account consists of the SID of the administrator account, minus the RID, which is omitted last 3 bit or 4 bit (500).

Query to get my own user SID

If a user’s SID is to be specifically queried, such as his own user SID, this can be done with the following command.

wmic useraccount where name='%username%' get name,sid

If you want to know another user’s SID, you can specify a user instead of %username%, e.g., john.

The following command detects the SID of the user who is currently logged on in an AD domain.

wmic useraccount where (name='%username%' and domain='%userdomain%') get domain,name,sid

In the opposite way, it is also possible to query the user name of a SID.

wmic useraccount where sid='S-1-5-21-4147432549-3588766049-1627529166-1001' get name

Get user SID in the PowerShell

In the PowerShell, the get user SID command looks like this.

[wmi] "win32_userAccount.Domain='$env:UserDomain',Name='$env:UserName'"

The user name and SID of the user logged on to the company domain is output.

PS C:\>[wmi] "win32_userAccount.Domain='$env:UserDomain',Name='$env:UserName'"

AccountType : 512
Caption: company-john
Domain : company
SID : S-1-5-21-4147432549-3588766049-1627529166-1001
FullName : john smith
Name : john

For example, you can use the user SID to find the ProfileImagePath for the user profile in the registry in order to make repairs or adjustments. The user SID is also used as an ObjectID in SQL tables to identify and authorize users from Active Directory in an application, such as Dynamics AX.

Get User SID in Command Prompt or PowerShell. you can use the SID to find the ProfileImagePath for the user profile in the registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\NT-CurrentVersion\ProfileList

Set-Clipboard, Get-Clipboard in PowerShell, How to use

How to use Set-Clipboard and Get-Clipboard in PowerShell

Set-Clipboard in PowerShell

PowerShell provide a cmdlet for use the clipboard, advanced applications are allowed, from PowerShell save multible items to the clipboard to use later, it is possible to append content to already pasted items in the clipboard.

Use Set-Clipboard in PowerShell

Use the PowerShell cmdlet Set-Clipboard to copy a text to the clipboard.

Set-Clipboard "but this to clipboard.."

Use Get-Clipboard in PowerShell

Use the text you just copied can be retrieved with Get-Clipboard.

Get-Clipboard

Use Set-Clipboard with -Append copy a text to the existing one.

Write-Output "append something to" | Set-Clipboard -Append
Get-Clipboard

This the query again with Get-Clipboard.

Delete clipboard content

The following command deletes the contents on the clipboard.

Write-Host "delete items in clipboard.." $null | clip

Retrieve files and folders from Clipboard

And there is even more to go, if you want to retrieve files and folders that are to be copied from the clipboard, the following command gives the output.

Get-Clipboard -Format FileDropList
PowerShell Get-Clipboard -Format FileDropList

It is also useful to copy the text content of files directly without opening the file to clipboard, such as scripts or source code.

Use Get-Content with clipboard

Get-Content copies the content of test.bat to the clipboard.

Get-Content test.bat | clip

Compare-Object compares the content and copies it to the clipboard.

Compare-Object $(Get-Content c:\temp\test.bat) $(Get-Content c:\temp\test1.bat) | clip

The default of Compare-Object is not case sensitive, use the parameter -CaseSensitive to distinguish small and capital letters.

Help with examples for use can be found with Get-Help.

Get-Help Set Clipboard -Detailed
Get-Help Get Clipboard -Detailed

Remarks

Commonly, in Windows the text or content is copied to the clipboard by pressing the “CTRL+C” shortcut key. However, in PowerShell, the text can also be copied to the clipboard using the “Set-Clipboard” cmdlet. This cmdlet sets the content to the clipboard. Moreover, the copied cmdlets can be pasted too in PowerShell using the “Get-Clipboard” cmdlet.

The following post will elaborate on the method to copy the content to the clipboard.
Using PowerShell Copy to Clipboard Function

As described earlier, the copy to clipboard function in PowerShell sets the text or content to the clipboard. The command used for that purpose is the “Set-Clipboard”. Examples explaining the procedure to copy the text to the clipboard are explained.