Misbehaving Get-ChildItem Operation in PowerShell

Question:

There is a back-end SQL DB contains “managed folders” in the form of UNC paths. Using SQL queries in PowerShell I have a loop that will work it’s way through these folders and run a GCI operation against them to work out how much disk space they are using.

For the sake of the question, $managedFolder is declared as above. The failing command below:

Now if I were to run this command manually in PS console it’s fine, it pulls back data. But as soon as it’s packaged in a script, it fails with the below error. The folder is accessible from the server, as it works fine from a local PS console session.

I’m stumped.

Answer:

The problem with your path is that it does not have any indication about which provider to use, so PowerShell just use current one. And if current provider is not a file system provider, then it will fail. So you need to specify provider in path, to allow PowerShell to choose right one regardless of current provider:

Source:

Misbehaving Get-ChildItem Operation in PowerShell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply