Bobeliño
Bobeliño

Reputation: 13

Why can't I add a type with my 'new-item' command in powershell?

I'm new to windows powershell. Today I tried the 'new-item' command and it should let met choose a path and a type.

First I entered c:\newpath (in path[0]:) Then I just pressed enter (in path[1]:)

But then it creates a file with the name newpath in c:\

But I want to add the type: directory. But it doesn't show the (Type:) in the terminal.

What am I doing wrong?

Upvotes: 1

Views: 819

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25031

You can use the -ItemType parameter in your command to specify a directory.

New-Item -ItemType Directory -Path C:\NewPath

Explanation:

The New-Item command has two parameter sets. The default parameter set is pathSet. That parameter set only requires one parameter, which is Path. So it will never prompt for more than that. The command below will list the parameter sets and definitions for New-Item.

Get-Command New-Item -ShowCommandInfo

Name          : New-Item
ModuleName    : Microsoft.PowerShell.Management
Module        : @{Name=Microsoft.PowerShell.Management}
CommandType   : Cmdlet
Definition    :
                New-Item [-Path] <string[]> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential
                <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]

                New-Item [[-Path] <string[]>] -Name <string> [-ItemType <string>] [-Value <Object>] [-Force]
                [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]

ParameterSets : {@{Name=pathSet; IsDefault=True; Parameters=System.Management.Automation.PSObject[]}, @{Name=nameSet;
                IsDefault=False; Parameters=System.Management.Automation.PSObject[]}}

Notice IsDefault=True for the hash table for pathSet. If you provide no parameters with a command, PowerShell will attempt to resolve the default parameter set by prompting for mandatory parameters. The code below will show the parameters for the pathSet parameter set and their mandatory settings.

(Get-Command New-Item -ShowCommandInfo).ParameterSets[0].Parameters | select Name,IsMandatory

Name                IsMandatory
----                -----------
Path                       True
ItemType                  False
Value                     False
Force                     False
Credential                False
Verbose                   False
Debug                     False
ErrorAction               False
WarningAction             False
InformationAction         False
ErrorVariable             False
WarningVariable           False
InformationVariable       False
OutVariable               False
OutBuffer                 False
PipelineVariable          False
WhatIf                    False
Confirm                   False
UseTransaction            False

An alternative way to list parameter info along with their corresponding parameter sets is (Get-Command New-Item -All).ParameterSets.

Upvotes: 3

Related Questions