Export (0) Print
Expand All

New-Item

Published: February 29, 2012

Updated: August 15, 2012

Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0

New-Item

Creates a new item.

Aliases

The following abbreviations are aliases for this cmdlet:

  • ni

Syntax

Parameter Set: pathSet
New-Item [-Path] <String[]> [-Credential <PSCredential> ] [-Force] [-ItemType <String> ] [-Value <Object> ] [-Confirm] [-WhatIf] [-UseTransaction] [ <CommonParameters>]

Parameter Set: nameSet
New-Item [[-Path] <String[]> ] -Name <String> [-Credential <PSCredential> ] [-Force] [-ItemType <String> ] [-Value <Object> ] [-Confirm] [-WhatIf] [-UseTransaction] [ <CommonParameters>]




Detailed Description

The New-Item cmdlet creates a new item and sets its value. The types of items that can be created depend upon the location of the item. For example, in the file system, New-Item is used to create files and folders. In the registry, New-Item creates registry keys and entries.

New-Item can also set the value of the items that it creates. For example, when creating a new file, New-Item can add initial content to the file.

Parameters

-Credential<PSCredential>

Specifies a user account that has permission to perform this action. The default is the current user.

Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

This parameter is not supported by any providers installed with Windows PowerShell


Aliases

none

Required?

false

Position?

named

Default Value

Current user

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Force

Allows the cmdlet to create an item that writes over an existing read-only item. Implementation varies from provider to provider. For more information, see about_Providers. Even using the Force parameter, the cmdlet cannot override security restrictions.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-ItemType<String>

Specifies the provider-specified type of the new item.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Name<String>

Specifies the name of the new item.

You can specify the name of the new item in the Name or Path parameter value, and you can specify the path to the new item in the Name or Path parameter value.


Aliases

none

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Path<String[]>

Specifies the path to the location of the new item. Wildcards are permitted.

You can specify the name of the new item in the Name parameter, or include it in the Path parameter.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

true

-Value<Object>

Specifies the value of the new item. You can also pipe a value to New-Item.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-Confirm

Prompts you for confirmation before running the cmdlet.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-UseTransaction

Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_Transactions


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • System.Object

    You can pipe a value for the new item to the New-Item cmdlet.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • System.Object

    New-Item returns the item that it creates.


Notes

  • The New-Item cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, type "Get-PsProvider". For more information, see about_Providers.

Examples

-------------------------- EXAMPLE 1 --------------------------

This command creates a text file named testfile1.txt in the current directory. The dot (.) in the value of the Path parameter indicates the current directory. The quoted text that follows the Value parameter is added to the file as content.


PS C:\> new-item -path . -name testfile1.txt -itemtype "file" -value "This is a text string."

-------------------------- EXAMPLE 2 --------------------------

This command creates a directory named Logfiles in the C: drive. The ItemType parameter specifies that the new item is a directory, not a file or other file system object.


PS C:\> new-item -path c:\ -name logfiles -itemtype directory

-------------------------- EXAMPLE 3 --------------------------

This command creates a Windows PowerShell profile in the path that is specified by the $profile variable.

You can use profiles to customize Windows PowerShell. $Profile is an automatic (built-in) variable that stores the path and file name of the CurrentUser/CurrentHost profile. By default, the profile does not exist, even though Windows PowerShell stores a path and file name for it.

In this command, the $profile variable represents the path to the file. The ItemType parameter specifies that the command creates a file. The Force parameter lets you create a file in the profile path, even when the directories in the path do not exist (Windows PowerShell creates them).

After you use this command to create a profile, you can enter aliases, functions, and scripts in the profile to customize your shell.

For more information, see about_Automatic_Variables and about_Profiles.1


PS C:\> new-item -path $profile -itemtype file -force

-------------------------- EXAMPLE 4 --------------------------

This command creates a new Scripts directory in the C:\PS-Test directory.

The name of the new directory item, Scripts, is included in the value of the Path parameter, instead of being specified in the value of the Name parameter. As indicated by the syntax, either command form is valid.


PS C:\> new-item -itemtype directory -path c:\ps-test\scripts

-------------------------- EXAMPLE 5 --------------------------

This command uses the New-Item cmdlet to create files in two different directories. Because the Path parameter takes multiple strings, you can use it to create multiple items.


PS C:\> new-item -itemtype file -path "c:\ps-test\test.txt", "c:\ps-test\Logs\test.log"

Related topics



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft