Bajan
Bajan

Reputation: 694

Why is my powershell script unable to create a folder in C:\Program Files?

I am trying to create a script which will create a directory in the C:\Program Files folder.

I am able to run the line to create it fine when I run in via an elevated powershell prompt manually - however when I run it via a script (elevated still) it simply creates a "file" object with the name I want instead of a folder.

Here is the code I am using to write the folder:

New-Item -ItemType Directory -Force -Path "C:\Program Files\Info"

I have also tried simply copying a folder into the Program Files location, with the same results.

Is there anyway I can script this so it will work?

I have found an old vbscript which does work to create the folder, so there must be a way to accomplish this in Powershell!?

Thanks for your time

Upvotes: 1

Views: 5121

Answers (1)

Bajan
Bajan

Reputation: 694

The issue was actually my mistake, before creating the directory there was a block of code with Copy-Item to the non-existent directory without the trailing slash, which produced the result in which instead of the parent folder being created as expected, a file with the folder name was created, with the type "file".

Creating the directory before copying items solved the issue. (Or including the trailing slashed and forcing the New-Item might have worked also)

Hopefully this might be useful to someone in the future!

Upvotes: 1

Related Questions