Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 464 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use FileInfo object from Powershell

#1
I am now starting to use PowerShell and after a lot of time using the Unix shells and want to know how to check for the existence of a file or directory.

In Powershell why does `Exist` return false in the following expression?

PS H:\> ([System.IO.FileInfo]"C:\").Exists
False

And is there a better way to check if a file is a directory than:

PS H:\> ([System.IO.FileInfo]"C:\").Mode.StartsWith("d")
True

Reply

#2
> In Powershell why does Exist return false in the following expression?
>
> <PRE>
> PS H:> ([System.IO.FileInfo]"C:\").Exists
> </PRE>

Because there is no file called "C:\" - it's a directory.
Reply

#3
In addition to [Michael's answer][1] you could also test using:


PS H:> ([System.IO.DirectoryInfo]"C:\").Exists
True



[1]:

[To see links please register here]

Reply

#4
You can use `Get-Item` to allow PowerShell to select between `FileInfo` and `DirectoryInfo`. It will throw an exception if the path doesn't resolve to a location.

PS> $(Get-Item "C:\").GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True DirectoryInfo System.IO.FileSystemInfo

I would only use this over `Test-Path` if you will need the `DirectoryInfo` or `FileInfo` entry if it does exist.
Reply

#5
Help Test-Path

Test-Path Determines whether all elements of a path exist

Test-Path -PathType Leaf C:\test.txt
Test-Path -PathType Container C:\
Test-Path C:\
Reply

#6
Both of these evaluate to true

$(Get-Item "C:\").GetType() -eq [System.IO.DirectoryInfo]
$(Get-Item "C:\test.txt").GetType() -eq [System.IO.FileInfo]
Reply

#7
I see the best answer above using get-item, but original question used [System.IO.FileInfo] so I'll pose an answer.

[System.IO.FileInfo]"C:\Users\carsonk" | select *

Above will show most of the available attributes not shown in the default table view. There are some PS built-in attributes such as PSIsContainer that won't show by select *

PSIsContainer is not available with [System.IO.FileInfo] so

If(([System.IO.FileInfo]"C:\Users\carsonk").directory) {$True}
True

Reply

#8
Use `Test-Path` instead of `System.IO.FileInfo.Exists`:

PS> Test-Path -Path 'C:\'
True

You can also use `-PathType` to test whether the location is a file or directory:

PS> Test-Path -Path 'C:\' -PathType Container
True

PS> Test-Path -Path 'C:\' -PathType Leaf
False

`DirectoryInfo` and `FileInfo` also both define a `PSIsContainer` property:

PS> (Get-Item -Path 'C:\').PSIsContainer
True

PS> (Get-Item -Path 'C:\windows\system32\notepad.exe').PSIsContainer
False
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through