4

I'm dabbling in PowerShell again. My experience thus far has been limited to file operations typically found in old school DOS batch files (build folder structures, copy files, append to a file, etc.).

The following outputs a property label ("TotalSeconds") and its property value ("12.3456").

Measure-Command { c:\_foo\test.txt c:\_bar } | select TotalSeconds

How do I output only the property value ("12.3456")?

Thanks!

3 Answers 3

7

Wrap the whole command in parentheses, and use the dot operator to access the property name.

Give this a shot:

(Measure-Command { c:\_foo\test.txt c:\_bar } | select TotalSeconds).TotalSeconds;

Or even more simply:

(Measure-Command { c:\_foo\test.txt c:\_bar }).TotalSeconds;
Sign up to request clarification or add additional context in comments.

1 Comment

Also: measure-command { ... } | select-object -expandproperty TotalSeconds
5

The Select-Object cmdlet has an -ExpandProperty parameter that will return the property's value:

Measure-Command { c:\_foo\test.txt c:\_bar } | 
    Select-Object -ExpandProperty TotalSeconds

Comments

1

You can use the -ExpandProperty option on the Select-Object cmdlet. Here's a real-life example.

I want to add cl.exe from the MSVC toolchain to my path, so I can compile C programs from the Windows CMD prompt and PowerShell. I never remember the full path because it's buried in the application folder.

I do remember the path to the application folder though, so I assign it to a variable and use Get-ChildItem and Select-Object with a few options you may not have seen yet*.

_

Here is the example:

<0.o> $d="c:\program files (x86)\microsoft visual studio"

<0.o> get-childitem -path $d -filter 'cl.exe' -force -recurse -erroraction silentlycontinue|select -expandproperty directory|select -expandproperty fullname

C:\program files (x86)\microsoft visual studio\2019\Community\SDK\ScopeCppSDK\vc15\VC\bin
C:\program files (x86)\microsoft visual studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx64\x64
C:\program files (x86)\microsoft visual studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx64\x86
C:\program files (x86)\microsoft visual studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx86\x64
C:\program files (x86)\microsoft visual studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx86\x86

This task confused me at first, because I tried to go straight from this:

> get-childitem -path $y -filter "cl.exe" -recurse -erroraction silentlycontinue -force|select directory


Directory : C:\program files (x86)\microsoft visual 
            studio\2019\Community\SDK\ScopeCppSDK\vc15\VC\bin

Directory : C:\program files (x86)\microsoft visual 
            studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx64\x64

Directory : C:\program files (x86)\microsoft visual 
            studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx64\x86

Directory : C:\program files (x86)\microsoft visual 
            studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx86\x64

Directory : C:\program files (x86)\microsoft visual 
            studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\Hostx86\x86

to this:

> get-childitem -path $y -filter "cl.exe" -recurse -erroraction silentlycontinue -force|select -expandtab directory



Name           : bin
CreationTime   : 7/26/2021 12:47:05 AM
LastWriteTime  : 7/26/2021 12:47:06 AM
LastAccessTime : 9/29/2021 9:32:55 AM
Mode           : d-----
LinkType       : 
Target         : {}

Name           : x64
CreationTime   : 7/26/2021 12:49:02 AM
LastWriteTime  : 7/26/2021 12:50:09 AM
LastAccessTime : 9/29/2021 9:32:55 AM
Mode           : d-----
LinkType       : 
Target         : {}

Name           : x86
CreationTime   : 7/26/2021 12:49:02 AM
LastWriteTime  : 7/26/2021 12:50:09 AM
LastAccessTime : 9/29/2021 9:32:55 AM
Mode           : d-----
LinkType       : 
Target         : {}

Name           : x64
CreationTime   : 7/26/2021 12:48:55 AM
LastWriteTime  : 7/26/2021 12:50:09 AM
LastAccessTime : 9/29/2021 9:32:55 AM
Mode           : d-----
LinkType       : 
Target         : {}

Name           : x86
CreationTime   : 7/26/2021 12:48:54 AM
LastWriteTime  : 7/26/2021 12:50:09 AM
LastAccessTime : 9/29/2021 9:32:55 AM
Mode           : d-----
LinkType       : 
Target         : {}


Yikes! Adding -ExpandTab to my previous command didn't give me what I wanted. Let's back up and use the Get-Module command (gm) to look at the objects in my pipeline:

> get-childitem -path $y -filter "cl.exe" -recurse -erroraction silentlycontinue -force|select -expandproperty directory|gm




   TypeName: System.IO.DirectoryInfo

Name                      MemberType     Definition                                   
----                      ----------     ----------                                   
LinkType                  CodeProperty   System.String LinkType{get=GetLinkType;}     
Mode                      CodeProperty   System.String Mode{get=Mode;}                
Target                    CodeProperty   System.Collections.Generic.IEnumerable`1[[...
Create                    Method         void Create(), void Create(System.Security...
CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRe...
CreateSubdirectory        Method         System.IO.DirectoryInfo CreateSubdirectory...
Delete                    Method         void Delete(), void Delete(bool recursive)   
EnumerateDirectories      Method         System.Collections.Generic.IEnumerable[Sys...
EnumerateFiles            Method         System.Collections.Generic.IEnumerable[Sys...
EnumerateFileSystemInfos  Method         System.Collections.Generic.IEnumerable[Sys...
Equals                    Method         bool Equals(System.Object obj)               
GetAccessControl          Method         System.Security.AccessControl.DirectorySec...
GetDirectories            Method         System.IO.DirectoryInfo[] GetDirectories()...
GetFiles                  Method         System.IO.FileInfo[] GetFiles(string searc...
GetFileSystemInfos        Method         System.IO.FileSystemInfo[] GetFileSystemIn...
GetHashCode               Method         int GetHashCode()                            
GetLifetimeService        Method         System.Object GetLifetimeService()           
GetObjectData             Method         void GetObjectData(System.Runtime.Serializ...
GetType                   Method         type GetType()                               
InitializeLifetimeService Method         System.Object InitializeLifetimeService()    
MoveTo                    Method         void MoveTo(string destDirName)              
Refresh                   Method         void Refresh()                               
SetAccessControl          Method         void SetAccessControl(System.Security.Acce...
ToString                  Method         string ToString()                            
Attributes                Property       System.IO.FileAttributes Attributes {get;s...
CreationTime              Property       datetime CreationTime {get;set;}             
CreationTimeUtc           Property       datetime CreationTimeUtc {get;set;}          
Exists                    Property       bool Exists {get;}                           
Extension                 Property       string Extension {get;}                      
FullName                  Property       string FullName {get;}                       
LastAccessTime            Property       datetime LastAccessTime {get;set;}           
LastAccessTimeUtc         Property       datetime LastAccessTimeUtc {get;set;}        
LastWriteTime             Property       datetime LastWriteTime {get;set;}            
LastWriteTimeUtc          Property       datetime LastWriteTimeUtc {get;set;}         
Name                      Property       string Name {get;}                           
Parent                    Property       System.IO.DirectoryInfo Parent {get;}        
Root                      Property       System.IO.DirectoryInfo Root {get;}          
BaseName                  ScriptProperty System.Object BaseName {get=$this.Name;}     


I spy a FullName property that looks promising, bringing me to the final version I showed up top.

_

*For options you have not seen, pop open a powershell and type:

get-help get-childitem -showwindow

or

get-help select-object -showwindow

and use the search bar at the top of the window that appears to search for the option you want.

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.