Pregunta

This post for using get-childitem to find last logged on user was quite helpful, Powershell - last logged on user - same input, different output

However, I am running into some problems.

I am writing the following command:

Get-Childitem c:\users\*\ntuser.dat -force | select-object -last 1 {(Split-path $_.directory -leaf)},last* | sort lastwritetime -descending 

It gives output in the form

(Split-path $_.directory -leaf) : jjoe
LastAccessTime                  : 10/10/2013 11:37:14 AM
LastAccessTimeUtc               : 10/10/2013 3:37:14 PM
LastWriteTime                   : 10/10/2013 1:51:02 PM
LastWriteTimeUtc                : 10/10/2013 5:51:02 PM

How do I make this output jjoe instead of all those lines?

I tried

$abc = Get-Childitem c:\users\*\ntuser.dat -force | select-object -last 1 {(Split-path $_.directory -leaf)},last* | sort lastwritetime -descending 

$abc.(Split-path $_.directory -leaf)

But it doesn't work.

Thank you!

¿Fue útil?

Solución

Replace this:

{(Split-path $_.directory -leaf)},last*

With this:

@{ n='Username'; e={ Split-path $_.directory -leaf } },last*

Then the property is named on the pipeline and you can then do:

<your script> | Select -Expand UserName

Explanation: @{Name=$StringVaue; Expression=$ScriptBlock} is how you can add Calculated Properties to Select-Object. Short Form @{n=$StringValue; e=$ScriptBlock}

Otros consejos

You could also get the name without Split-Path. The Directory property is a System.IO.DirectoryInfo object so you could get its name property:

... @{n='Username'; e={$_.Directory.Name }}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top