YashSeeta
YashSeeta

Reputation: 63

Powershell : New-Item -name < >

Scenario : a folder named "x" is created in a certain path

I'm trying to see if the following is possible :

if i can create a file named "x" in the same path where "x" folder is present without specifying any extension in the name parameter.

I used the command new-item -name x -path < > -force

Output: its throwing an error and am not able to create a file with same name

However , if i specify the extension ".txt" am able to do it.

I tried creating the file first and then the folder, but am getting another error. I used the force parameter to create the folder. There is no error , but when i check the path for the folder i dont see it ! The file is there though.

I would like to know why it is failing and is there a workaround for this?

FOLDER CREATED FIRST , THEN CREATING FILE Error

PS C:\Windows\system32> New-Item -name Test1 -path E:\ -ItemType directory 

New-Item -name Test1 -path E:\ -ItemType file -force


        Directory: E:\


    Mode                LastWriteTime     Length Name                                                                                    
    ----                -------------     ------ ----                                                                                    
    d----        16-05-2016     20:20            Test1                                                                                   
    New-Item : Access to the path 'E:\Test1' is denied.
    At line:3 char:1
    + New-Item -name Test1 -path E:\ -ItemType file -force
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (E:\Test1:String) [New-Item], UnauthorizedAccessException
        + FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

FILE CREATED FIRST , THEN CREATING FOLDER ERROR

PS C:\Windows\system32> 
New-Item -name Test2 -path E:\ -ItemType file -force


    Directory: E:\


Mode                LastWriteTime     Length Name                                                                                    
----                -------------     ------ ----                                                                                    
-a---        16-05-2016     20:26          0 Test2                                                                                   



PS C:\Windows\system32> New-Item -name Test2 -path E:\ -ItemType directory 
New-Item : An item with the specified name E:\Test2 already exists.
At line:1 char:1
+ New-Item -name Test2 -path E:\ -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (E:\Test2:String) [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

Upvotes: 0

Views: 1337

Answers (1)

iTayb
iTayb

Reputation: 12743

Files and folders on all major filesystems can't share the same name. A directory is a special type of file that from the user's perspective can "contain" other files.

Upvotes: 6

Related Questions