Converting Get-ChildItem output to string[]
Logical answer:
You are casting the FileInfo
objects to strings. When you do gci path
, you have files and directories only from that folder. So it is ok to just convert to the name of the item.
When you are using gci path\*
, it is across folders, and just converting to names is not valid, so it includes the path.
Depending on whether you want name or the full path, do this:
gci $path | select -expand Name
or
gci $path | select -expand FullName
Related videos on Youtube
Nikhil Dabas
Updated on September 14, 2022Comments
-
Nikhil Dabas over 1 year
I have a script that accepts a list of paths to process as an array of strings. I'm using
Get-ChildItem
to generate those paths. I've come across some interesting behavior in the conversion fromSystem.IO.FileInfo
objects to strings.Example 1:
PS C:\Users\Nikhil\Documents> [string[]](Get-ChildItem .\GitHub\) toc toc-gh-pages Publish gh-pages.txt
Example 2:
PS C:\Users\Nikhil\Documents> [string[]](Get-ChildItem .\GitHub\*) C:\Users\Nikhil\Documents\GitHub\toc C:\Users\Nikhil\Documents\GitHub\toc-gh-pages C:\Users\Nikhil\Documents\GitHub\Publish gh-pages.txt
Example 3: (With -Recurse, so the conversion to strings is illogical and useless)
PS C:\Users\Nikhil\Documents\GitHub> [string[]](Get-ChildItem .\toc-gh-pages -Recurse) assets css lib _layouts _site .gitattributes .gitignore index.html _config.yml jquery.toc.zip docs.less docs.min.css google-code-prettify jquery.toc lang-apollo.js ...
Example 4:
PS C:\Users\Nikhil\Documents\GitHub> [string[]](Get-ChildItem .\toc-gh-pages\*.* -Recurse) C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\assets\jquery.toc.zip C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\css\docs.less C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\css\docs.min.css C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-apollo.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-basic.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-clj.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-css.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-dart.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-erlang.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-go.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-hs.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-lisp.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-llvm.js C:\Users\Nikhil\Documents\GitHub\toc-gh-pages\lib\google-code-prettify\lang-lua.js ...
The addition of a wildcard to the path causes the resulting strings to be full path names, instead of just file/folder names. Why does this happen?
I understand how I can work around this behavior and get the info that I need; what I'm interested in is why this is happening, given that we're just converting
System.IO.FileInfo
andSystem.IO.DirectoryInfo
objects to strings in all cases.-
CB. over 10 yearsRead here: stackoverflow.com/a/2744637/520612
-
CB. over 10 yearspossible duplicate of Mysterious different conversion to string[] of seemingly same input data
-
-
Nikhil Dabas over 10 yearsThat might seem logical, but the same thing happens if I include the -Recurse switch. So the output in that case is completely useless -- just a bunch of file names with no directory names.