Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4536700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:34:18+00:00 2026-05-21T14:34:18+00:00

Sorry for the title. I’ll try to explain better. Let’s suppose that I run

  • 0

Sorry for the title. I’ll try to explain better.

Let’s suppose that I run this command in order to get all paths of a directory and I want to redirect them to a text file.

gci e:\mytree -r | % {$_.fullname} | out-file e:\folderstructure.txt

Now I need to recreate this nested structure using new-item cmdlet.

Let’s suppose now that I run this command:

gc e:\folderstructure.txt | % {
[system.io.fileinfo]$info = $_
write-host $info $info.extension
}

that produces this output:

E:\mytree\folder1 
E:\mytree\folder2 
E:\mytree\folder3 
E:\mytree\file1.txt .txt
E:\mytree\file12.txt .txt
E:\mytree\folder1\folder.with.dots .dots
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt .txt
E:\mytree\folder3\file4.doc .doc

As you can see folder.with.dots is a folder but “it’s seen” as a file (it gives me .dots extension) because it contains dots within name. If I don’t know all possible extensions of my files is there any property that can tell me if an object iscontainer or not so that I can use new-item with the right switch file or directory to create it?

I hope you have understood my problem despite my English. Thanks in advance.

edit. UPDATE after JPBlanc answer

Thank you very much. 🙂

I was trying in this way:

gc e:\folderstructure.txt | % {
[system.io.directoryinfo]$info = $_
if ($info.psiscontainer) {
write-host "$info is a folder" }
else {
write-host "$info is a file" }
}

and the output was:

E:\mytree\folder1 is a file
E:\mytree\folder2 is a file
E:\mytree\folder3 is a file
E:\mytree\file1.txt is a file
E:\mytree\file12.txt is a file
E:\mytree\folder1\folder.with.dots is a file
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt is a file
E:\mytree\folder3\file4.doc is a file

Following your advice:

gc e:\folderstructure.txt | % {
[system.io.directoryinfo]$info = $_
if ((get-item $info).psiscontainer) {
write-host "$info is a folder" }
else {
write-host "$info is a file" }
}

E:\mytree\folder1 is a folder
E:\mytree\folder2 is a folder
E:\mytree\folder3 is a folder
E:\mytree\file1.txt is a file
E:\mytree\file12.txt is a file
E:\mytree\folder1\folder.with.dots is a folder
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt is a file
E:\mytree\folder3\file4.doc is a file

everything works fine. Now I can achieve my goal. Thanks again.

LAST EDIT.
I had another idea. I decided to check if an object is a file or a folder before creating txt file. After some difficulties (for example I’ve soon discovered that I can’t redirect format-table that I was using to hide table headers to export-csv
http://blogs.msdn.com/b/powershell/archive/2007/03/07/why-can-t-i-pipe-format-table-to-export-csv-and-get-something-useful.aspx )
I came up with this solution:

gci e:\mytree -r | 
select fullname,@{n='folder';e={ switch ($_.psiscontainer) { 
                                                                    true {1} 
                                                                    false {0}  
                                                                 }
                                     }
                     } | % {($_.fullname,$_.folder) -join ","} | out-file e:\structure.txt

that gets me this output:

E:\mytree\folder1,1
E:\mytree\folder2,1
E:\mytree\folder3,1
E:\mytree\file1.txt,0
E:\mytree\file12.txt,0
E:\mytree\folder1\folder.with.dots,1
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt,0
E:\mytree\folder3\file4.doc,0

So I can easily split two parameters and use new-item cmdlet accordingly to object type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:34:19+00:00Added an answer on May 21, 2026 at 2:34 pm

    Both FileInfo type an DirectoryInfo type has got a property PSIsContainer, that allow you to see if the object is a directory or not.

    PS C:\temp> (Get-Item 'Hello world.exe').PSIsContainer
    False
    PS C:\temp> (Get-Item 'c:\temp').PSIsContainer
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, sorry for the title. Someone please propose a better one, I
Sorry for the vague title, I didn't know how else to phrase this. Suppose
sorry for that title but it is very hard to explain in a few
Sorry about the title, I'm not exactly sure how to explain this with a
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry for the bad title, but I couldn't think of a better one. I'm
Sorry for the slightly rubbish title. I could not think how to describe this
I'm sorry I could not think of a better title. The problem is the
Sorry the title is not very clear. This is a follow up to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.