PowerShell New-Item Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Clear-Content cmdlet in PowerShell.
https://cloudaffaire.com/powershell-clear-content-cmdlet/
In this blog post, we will discuss New-Item cmdlet in PowerShell. You can use New-Item cmdlet to create new files and directories in PowerShell. New-Item cmdlet can also be used to create new symbolic links, hard links, registry keys, etc by assigning appropriate value to “ItemType” argument.
New-Item Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
## New-Item ## [[-Path] ## -Name ## [-ItemType ## [-Value ## [-Force] ## [-Credential ## [-WhatIf] ## [-Confirm] ## [-UseTransaction] ## [ |
New-Item Cmdlet Argument List:
- –Confirm: Prompts you for confirmation before running the cmdlet.
- –Credential: To impersonate another user or elevate your credentials.
- –Force: Forces this cmdlet to create an item that writes over an existing read-only item.
- –ItemType: Specifies the provider-specified type of the new item. An Item can be a File, Directory, SymbolicLink, Junction or HardLink.
- –Name: 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 of the new item in Name or Path value.
- –Path: Specifies the path of the location of the new item. The default is the current location when Path is omitted. You can specify the name of the new item in Name, or include it in Path.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
- –Value: Specifies the value of the new item. You can also pipe a value to New-Item.
- –WhatIf: Shows what would happen if the cmdlet runs. The cmdlet is not run.
PowerShell New-Item Cmdlet:
Create A New Directory In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#################################### ## PowerShell | Cmdlet | New-Item ## #################################### ## PowerShell Latest Version (5) ## create a new directory for this demo New-Item -ItemType Directory -Path C:\MyDir\ ## create a directory in PowerShell New-Item -ItemType Directory -Path C:\MyDir\ -Name MyDir1 Get-ChildItem -Path C:\MyDir |
Create A Chain Of Directories And Sub-Directories In PowerShell:
1 2 3 4 5 |
## create chain of directories in PowerShell New-Item -ItemType Directory -Path C:\MyDir\MyDir2\MySubDir1\MySubDir2 Get-ChildItem -Path C:\MyDir -Depth 3 |
Create Multiple Directories At Once In PowerShell:
1 2 3 4 5 |
## create multiple directories at once in PowerShell New-Item -ItemType Directory -Path "C:\MyDir\MyDir3","C:\MyDir\MyDir4" Get-ChildItem -Path C:\MyDir |
Create A New File In PowerShell:
1 2 3 4 5 |
## create a new file in PowerShell New-Item -ItemType File -Path C:\MyDir\ -Name MyFile1 Get-ChildItem -Path C:\MyDir |
Create A New File With Content In PowerShell:
1 2 3 4 5 |
## create a new file with content in PowerShell New-Item -ItemType File -Path C:\MyDir\ -Name MyFile2 -Value "Hello World" Get-ChildItem -Path C:\MyDir |
Create Multiple Files At Once In PowerShell:
1 2 3 4 5 6 7 8 9 |
## create multiple files at once in PowerShell New-Item -ItemType File -Path "C:\MyDir\MyFile3","C:\MyDir\MyFile4" Get-ChildItem -Path C:\MyDir ## remove the directory along with content Remove-Item -Force -Recurse C:\MyDir\ |
Hope you have enjoyed this article. In the next blog post, we will discuss Remove-Item cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation