Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Measure-Command in a powershell script?

I'm trying to write a little script to time commands :

PS C:\Users\myUserName> cat timeSeb.ps1
"=> args = $args"
( Measure-Command { $args | Out-Default } ).ToString()
PS C:\Users\myUserName>

But when I run it I don't see the list of files in the output :

PS C:\Users\myUserName> .\timeSeb.ps1 dir -force .smplayer
=> args = dir -force .smplayer
00:00:00.0026973
PS C:\Users\myUserName> 

EDIT0: If I type this command in an interactive powershell session, it works as expected :

PS C:\Users\myUserName> ( Measure-Command { dir -force .smplayer | Out-Default } ).ToString()


    Directory: C:\Users\myUserName\.smplayer


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        28/02/2024     18:14                file_settings
-a----        29/02/2024     11:01              8 favorites.m3u8
-a----        29/02/2024     11:01             77 hdpi.ini
-a----        22/02/2024     18:22          42405 player_info.ini
-a----        29/02/2024     11:01           1431 playlist.ini
-a----        29/02/2024     11:01              8 radio.m3u8
-a----        29/02/2024     11:01           8303 smplayer.ini
-a----        29/02/2024     11:01              8 tv.m3u8


00:00:00.0255996
PS C:\Users\myUserName> 

But from my timeSeb.ps1 script, it does not display the output of the dir -force command

I expect to see the output of the dir -force command.

like image 871
SebMa Avatar asked Jun 27 '26 22:06

SebMa


1 Answers

Alternatively, the history times every command:

start-sleep 5
get-history -count 1 | fl

Id                 : 18
CommandLine        : start-sleep 5
ExecutionStatus    : Completed
StartExecutionTime : 3/1/2024 10:56:36 AM
EndExecutionTime   : 3/1/2024 10:56:41 AM
like image 56
js2010 Avatar answered Jul 01 '26 00:07

js2010



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!