PowerShell for Windows Admins

Apr 26 2010   3:56PM GMT

Desktop of current logged on user



Posted by: Richard Siddaway
Tags:
Users

 

In this post http://itknowledgeexchange.techtarget.com/powershell/current-logged-on-user/ we discovered how to find the current logged on user.  I want to extend that a bit and add the information about that users desktop.

We start with the script in our earlier post.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
## get session process
$proc = Get-WmiObject -Class Win32_SessionProcess | 
select Antecedent -First 1
$filt = ($proc -split "=")[2] -replace ‘"’,  -replace "}",""

$ltype = DATA {
ConvertFrom-StringData -StringData @’
0 = System
2 = Interactive
3 = Network
4 = Batch
5 = Service
6 = Proxy
7 = Unlock
8 = NetworkCleartext
9 = NewCredentials
10 = RemoteInteractive
11 = CachedInteractive
12 = CachedRemoteInteractive
13 = CachedUnlock
‘@

}
## get logon session
$sess = Get-WmiObject -Class Win32_LogonSession -Filter "LogonId=’$filt’"

## get user
$query = "ASSOCIATORS OF {Win32_LogonSession.LogonId=’$filt’}" `
            + "WHERE ResultClass=Win32_UserAccount"
$user = Get-WmiObject -Query  $query

$query2 = "ASSOCIATORS OF {Win32_UserAccount.Domain=’$($user.Domain)’" `
  + ",Name=’$($user.Name)’} WHERE ResultClass=Win32_Desktop"

$desktop = Get-WmiObject -Query $query2
 
Add-Member -InputObject $sess -MemberType NoteProperty -Name User `
   -Value $($user.Caption) -PassThru |
Format-List AuthenticationPackage, LogonId,
@{Name="Logon Type"; Expression = {$ltype["$($_.LogonType)"]}},
@{Name="Time"; Expression = {$_.ConvertToDateTime($_.StartTime)}},
User
"Desktop Settings:"
$desktop | Format-List BorderWidth, Caption, CoolSwitch,
CursorBlinkRate, Description, DragFullWindows,
GridGranularity, IconSpacing, IconTitleFaceName,
IconTitleSize, IconTitleWrap, Name, Pattern,
ScreenSaverActive, ScreenSaverExecutable,
ScreenSaverSecure, ScreenSaverTimeout,
SettingID, Wallpaper, WallpaperStretched,
WallpaperTiled

We add a query to get the desktop associated with the current user  – - $query2

That query returns the information about the desktop. Its displayed after the user information and I’ve selected the items I want to display.

This is a good example of extending an existing script when you find out how to do a bit more digging.

I think we’ve about exhausted the WMI information on users and groups for now – so its time to find another topic.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: