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.
Measure-Commandcannot access$args, that's why it's not writing anything.UPDATE:
use this to run a command passed as parameters
(Measure-Command -InputObject "$args" { Invoke-Expression -Command $_ | Out-Default }).toString()OLD AND BUSTED:
You'd need to pass it like
(Measure-Command -InputObject $args -Expression { $_ | Out-Default }).toString()(or
($args | Measure-Command -Expression { $_ | Out-Default }).toString())Your other example has its values hard-coded in the string command, that0s why it works