Answered by:
PowerShell: Want to get the length of the string in output

Question
-
Hi All,
I am typing this but it is not working. Does anyone know what I could be doing wrong.
The command I wrote is:
GCI -file | foreach {$_.name} | sort-object length | format-table name, length
But it is not working. I am expecting a name of the file and length of the string like 8 characters etc. my file is called mystery so it should have 7 as its output of the name, length.
Thank-you
SQL 75
- Moved by Bill_Stewart Monday, May 18, 2015 5:20 PM Unanswerable drive-by question
Tuesday, February 10, 2015 6:42 AM
Answers
-
Get-ChildItem supports both -File and -Directory.
Help will help: https://technet.microsoft.com/library/hh847897(v=wps.630).aspx
Read the first couple of parameters to see.
GCI -file | sort-object length | format-table name, length | ft -auto
Seems to be a rasher of bad answers to day. YOu were just extracting the name property then trying to sort on a property that doesn't exist.Do the sort first then select the properties.
it helps to test answers before posting. I know because I get bit by posting without thinking to often. I have to remember to think first.
¯\_(ツ)_/¯
Tuesday, February 10, 2015 8:03 AM
All replies
-
Hi ,
your syntex is wrong .
there is no argument for GCI name "-file"
try get-help gci -full
or
get-help gci -example
if you remove -file you will get name of files try
GCI | foreach {$_.name.length} | sort-object length | format-table name, length
will give length of respected file wich will show by this command
GCI | foreach {$_.name} | sort-object length | format-table name, length
- Edited by Prahlad Yadav Tuesday, February 10, 2015 7:07 AM Completed
Tuesday, February 10, 2015 6:58 AM -
Get-ChildItem supports both -File and -Directory.
Help will help: https://technet.microsoft.com/library/hh847897(v=wps.630).aspx
Read the first couple of parameters to see.
GCI -file | sort-object length | format-table name, length | ft -auto
Seems to be a rasher of bad answers to day. YOu were just extracting the name property then trying to sort on a property that doesn't exist.Do the sort first then select the properties.
it helps to test answers before posting. I know because I get bit by posting without thinking to often. I have to remember to think first.
¯\_(ツ)_/¯
Tuesday, February 10, 2015 8:03 AM -
-File argument is present in PowerShell 4.0 not in 2.0. So i answred ....
Tuesday, February 10, 2015 9:12 AM -
-File argument is present in PowerShell 4.0 not in 2.0. So i answred ....
If someone posts with -File I think we should assume they have the version they claim to have. Besides 2.0 is pretty much obsolete.
File and Directory have been around for a couple of years now. Since WMF 3.0 about 4 years ago - December of 2011.
¯\_(ツ)_/¯
Tuesday, February 10, 2015 9:18 AM