Manage Learn to apply best practices and optimize your operations.

Why do I get a PowerShell error when trying to access a file?

One common PowerShell error occurs because of the way PowerShell is told how to access a file. Here's how to fix it and avoid it in the future.

Sometimes PowerShell throws back an error when trying to access a file. A bit of the problem depends on how you're...

telling PowerShell to access the file. Here's a snippet of code that illustrates what often goes wrong:

$files = Get-ChildItem c:\mydirectory -File
foreach ($file in $files) {
 $content = Get-Content $file
 $content –replace 'x','y'
 $content | Set-Content $file

The intent of the command is to replace all "x" characters with "y" in a bunch of text files, all of which are contained in C:\MyDirectory. The assumption is that every file in that directory is a text file and you want to do this to all of them.

The first problem lies in the following:

 $content = Get-Content $file 

The $file contains an entire file object, not just a file name. But Get-Content wants to see a filename.

Rather than just explaining the problem, one way I like to troubleshoot this error in Powershell is to have folks add debug output via Write-Verbose to their script:

$VerbosePreference = 'Continue'
$files = Get-ChildItem c:\mydirectory -File
foreach ($file in $files) {
 Write-Verbose "the file name is $file"
 $content = Get-Content $file
 $content –replace 'x','y'
 $content | Set-Content $file

Go ahead and run that. Change the C:\MyDirectory folder name to something appropriate on your system and add a couple of test text files to the folder to play with. Right away, the output will show you $file doesn't just contain a simple file name. It's a big, complex object with a lot of properties, one of which is the Name property.

 You might think, "Ah, here's the fix!" and run this code:

$VerbosePreference = 'Continue'
$files = Get-ChildItem c:\mydirectory -File
foreach ($file in $files) {
 Write-Verbose "the file name is $($file.name)"
 $content = Get-Content $file.name
 $content –replace 'x','y'
 $content | Set-Content $file.name
The moral here is to remember most PowerShell commands produce objects as their output, if they produce output at all.

As far as that goes, it will work. But the problem is that both Get-Content and Set-Content commands need more than just a file name; they need the entire location of the file. If you run the script from where the files are located in C:\MyDirectory, then this will work great. If you run the script from elsewhere, then Get-Content won't be able to locate the files, which creates a PowerShell error.

This is a good time to back off a bit. Try just running this:

Dir c:\mydirectory | Format-List *

The idea is to see what you're working with in that $file variable. What properties are available and what kind of data do they contain? Don't make assumptions; make a correction to your script based on reality. Would the Fullname property provide a complete path to the files? Or, do you need to concatenate some combination of Path and Name?

Rather than give you the answer, see if you can figure it out. That last command, assuming you point it to a valid directory that contains valid files on your system, is the key to solving this problem.

If it turns out concatenation is the answer, avoid using the + operator in PowerShell:

$name = $file.path + '\' + $file.name 

I find that hard to read, and so do most folks. Instead, rely on PowerShell's magical double quote marks and subexpressions:

$name = "$($file.path)\$($file.name)" 

One set of double quote marks, two $(subexpressions) and you're done. It may not be easier to read, but it leverages PowerShell's cool features.

The moral here is to remember most PowerShell commands produce objects as their output, if they produce output at all. The input parameters of a command, on the other hand, might not accept entire objects. You might need to access just one or two properties to get the information you need.

Don Jones is a well-known and respected PowerShell expert and educator. He's co-author of three books on PowerShell (see PowerShellBooks.com for a list). You can find his content online, including his PowerShell Q&A forums, by visiting DonJones.com.

This was last published in October 2012

Dig Deeper on Windows administration tools

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.