1

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

6
  • 1
    Can you please add the error that it returns? Commented Jun 23, 2017 at 18:02
  • 1
    -ItemType -> -Type Commented Jun 23, 2017 at 18:02
  • It does not give an error, it simply creates a "file" type unknown object with the name of the folder I wanted Commented Jun 23, 2017 at 18:03
  • @MathiasR.Jessen -ItemType works in POSH 5.x.x.x not sure about older versions Commented Jun 23, 2017 at 18:08
  • See if you can add a folder manually in that location,I feel it may be permission issue Commented Jun 23, 2017 at 18:12

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.