Windows PowerShell is a task-based command-line shell and scripting language developed by Microsoft for Windows operating systems. It provides a powerful interface for automating administrative tasks and managing system configurations, allowing users to automate repetitive tasks, manage large sets of data, and perform system-level operations with a single command or script.
PowerShell commands (known as cmdlets) can be used to manage and control virtually all aspects of the Windows operating system, including file and printer services, registry, process and service management, network management, and much more.
1. Create new File with New-Item cmdlet
The New-Item cmdlet is used to create new files in PowerShell. Here’s an example of how you can use this cmdlet to create a new text file:
Syntax: New-Item -Path <path> -Name <String> -ItemType <String> -Value <Object>
Run the below command to create a new file named newfile.txt with the value “Welcome to Techhyme” in the working directory (-Path .)
Command: New-Item -Path . -Name “newfile.txt” -ItemType “file” -Value “Welcome to Techhyme”
Open File Explorer, look for and open the text file (newfile.txt) you created in your preferred text editor to verify the file.
Now, run the below command to create multiple text files (file1.txt and file2.txt), where commas separate the paths (c:\).
Command: New-Item -ItemType “file” -Path “c:\file1.txt”, “c:\file2.txt”
Open File Explorer, look for two files (file1.txt and file2.txt) you created.
2. Creating Directories and Sub-directories
Besides creating files, the PowerShell New-Item cmdlet lets you create directories and sub-directories with a single command. This feature helps when creating a directory structure for your files.
Run the following command to create a new directory called techhyme in the C:\drive.
Command: New-Item -Path C:\ -Name “techhyme” -ItemType “directory”
Navigate to the C:\ drive in File Explorer, look for, and verify the newly-created directory (techhyme) exists.
You can also create a sub-directory by using the following command:
Command: New-Item -Path “C:\techhyme\tutorials” -ItemType “directory”
Lastly, navigate to the C:\techhyme directory to verify the newly-created sub-directory (tutorials) is present.
3. Creating Multiple Files with a Wildcard Character
Perhaps you are looking for a way to create text files for each week of the month in one go. In that case, a wildcard character works the magic, specifically the character.
The wildcard character can be helpful when you wish to create multiple files or directories with similar names.
Command: New-Item -Path C:\\techhyme\\* -Name files.txt -ItemType File | Select-Object FullName
Run the above command to create text files with the same name (files.txt) in all (*) existing sub-directories called folder1, folder2 and folder3. The [Select-Object] cmdlet is piped to the New-Item cmdlet to print the full path of each created log file.
Lastly, navigate to the C:\techhyme directory to verify the newly-created files (files.txt) in all sub-directories (folder1, folder2 and folder3).
4. Overwriting Files with the New-Item cmdlet
When you create a new item, such as a file or directory, with the PowerShell New-Item cmdlet, you will get an error message saying the item already exists, as shown below.
But perhaps, you wish to overwrite a log file each time a scheduled task executes. If so, appending the -Force parameter lets you forcefully overwrite files without getting a prompt or error message.
Run the below command to forcefully (-Force) overwrite the existing file (files.txt) with new content (New Content To Be Updated).
Command: New-Item -ItemType “file” -Path “c:\techhyme\folder1\files.txt” -Value ‘New Content To Be Updated’ -Force
Navigate to C:\techhyme\folder1\ directory and verify the content of files.txt.
You may also like:
- Using Elasticsearch Ingest Pipeline to Copy Data from One Field to Another
- Top 10 Useful Windows Commands
- Essential Commands For Process Management in Kali Linux
- How To Install Python 2.7.18 From The Source
- How To Parse SSH Authentication Logs with Logstash
- How To Easily Crack Wi-Fi Password
- 6 Most Useful Windows Command Prompt Commands
- Ripgrep – Searching for Specific File Types and Beyond
- Insert and Create Data in Elasticsearch
- Manage Time and Date in Linux with timedatectl