question

akosijesyang avatar image
0 Votes"
akosijesyang asked IanXue-MSFT commented

New-Item creates a file instead of directory/folder

Hi everyone -

I'm stuck with New-Item cmdlet creating a file instead of directory even though I specifically add -ItemType "directory"

Here's my code (inside of a -Scriptblock):
New-Item -Path "c:\" -Name "RXtemp" -ItemType "directory"

However when done using Invoke-Command, it works.
Invoke-Command -computername Target -scriptblock {New-Item -Path "c:\" -Name "RXtemp" -ItemType "directory"}

Am I missing something here?

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT commented

Hi,

As RXtemp is 186KB in your screenshot, please check if any other cmdlet in your script writes to the file, e. g., copy-item.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @IanXue-MSFT -

Yes, I have copy-item in my script. copy-item cmdlets are inside if/else statement, after c:\rxtemp folder is detected or created.

something like this
if (rxtemp is found) {copy-item}
else {create rxtemp then copy item}

Am I doing something wrong?

0 Votes 0 ·

Hi @akosijesyang ,

the full script would be helpful.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered akosijesyang commented

Hi @akosijesyang ,

I just tested your code in different variations:

 New-Item -Path "c:\" -Name "RXtemp1" -ItemType Directory
 New-Item -Path "c:\" -Name "RXtemp2" -ItemType "directory"

Both lines are creating a folder like expected.

My PS Version is 5.1.19041.906


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Andreas -

WOW! That's soooo weird.

Here's my version:


Name Value


PSVersion 5.1.18362.1474


0 Votes 0 ·

Hi @akosijesyang ,

the folder doesn't exist before you run the line?
And the same issue when the path is a subfolder, for instance `c:\temp`?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·

Hi @AndreasBaumgarten -

Here's what it created:
101094-image.png


and here's the actual code I'm using:
Invoke-Command -Session $psremote -ScriptBlock { New-Item -Path "c:\" -Name "RXtemp" -ItemType Directory}

the folder doesn't exist before you run the line? - yes. i want to create the folder 'cause I will be dropping some files there
And the same issue when the path is a subfolder, for instance c:\temp? - yes, the same
101086-image.png


0 Votes 0 ·
image.png (4.6 KiB)
image.png (12.5 KiB)
AdrianMartinez-0787 avatar image
0 Votes"
AdrianMartinez-0787 answered

Hello, I know that there are already answers but I think that you need to select the file you want to create
(There may be a pre set default)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.