Question

In PowerShell 3.0 I'm trying to right a script to give me a certain directory from an input parameter. The input parameter can be either relative or full path. I am having issues just passing it into the New-Object with a System.IO.DirectoryInfo because the Current working directory for PowerShell doesn't appear to match my current working directory for relative paths. So I tried the Get-ChildItem. If I run the command Get-ChildItem -Directory it will list out all the directories. In the results I see a folder named Queries. If I run Get-ChildItem -Directory "Queries". It returns nothing.

So two questions.

  1. Why does it do this and how do I get around it?
  2. What is the best way to get a DirectoryInfo object in PowerShell given that the path can be relative or rooted?
Was it helpful?

Solution

You may be better off using Resolve-Path instead. As for getting the Queries folder from GCI you probably want something along the lines of GCI .\ -Directory -Filter "Queries"

OTHER TIPS

Flip the tokens? Gci queries -directory

Because there is not a parameter for Get-ChildItem named -Directory .. The parameter you are looking for is -path

http://technet.microsoft.com/en-us/library/hh849800.aspx

Get-ChildItem -Path Queries will work

To get a System.Io.DirectoryInfo object just assign it to a variable

$queries = Get-ChildItem -Path Queries

I believe I have found an answer to this. If I use Get-Item "Queries" vs Get-ChildItem it should give me everything that is needed. Thank you for your help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top