New-Item creates a file instead of directory/folder

akosijesyang 101 Reputation points
2021-05-31T09:16:48.63+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,361 Reputation points Microsoft Vendor
    2021-06-01T02:49:59.177+00:00

    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.


2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 97,396 Reputation points MVP
    2021-05-31T10:58:05.537+00:00

    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


  2. Adrian Martinez 1 Reputation point
    2021-06-02T01:44:29.36+00:00

    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)

    0 comments No comments