File administration is a necessary a part of the on a regular basis routine as you will need to handle entry to shared folders, again up information and folders commonly, and do the opposite customary operations on information and folders in your Home windows system. The excellent news is you should use PowerShell to handle information simply. In truth, it will probably scale back the quantity of handbook work related to file administration. On this article, we’ll be specializing in how one can create information and folders in PowerShell
Making a file
To create a brand new file, use the New cmdlet. On the whole, this cmdlet is used to create any kind of object in PowerShell. All that you need to do is specify the kind of object you wish to create on this cmdlet.
Right here is how one can create a file or folder in PowerShell utilizing the New-Item command.
New-Merchandise -Path 'SharedTestFoldertestfile1.txt' - ItemType File
The above command will create a brand new file referred to as testfile1.txt. Right here, the Itemtype is a file, so this may create a file for you and the contents will probably be empty. In different phrases, this command will create an empty textual content file for you.
Creating information after checking for the file identify
Now if there was already a file with the identical identify, it is going to overwrite it. So, be sure you examine for the file identify earlier than you create a file. Another choice is to have a script that can routinely examine if one other file exists with the identical identify and can create a file provided that there isn’t any different file with that identify.
An instance of such a script is proven under.
$filenames = Get-ChildItem SharedTestFolder*.txt | choose -expand fullname $filenames -match "testfile1.txt" If ($filenames -eq 'False') New -Merchandise -Path 'SharedTestFoldertestfile1.txt' - ItemType File else
The above script will examine if a file with an identical identify exists in the identical listing and if that’s the case, it is going to exit. In any other case, it is going to create a brand new file.
Overwriting an present file
Earlier, we noticed keep away from overwriting an present file. However typically, you might have to overwrite a file and you are able to do that utilizing the Drive swap parameter.
New-Merchandise -Path 'SharedTestFoldertestfile1.txt' - ItemType File -Drive
This command will forcefully create a file and overwrite the prevailing one since you’re successfully telling the system that you already know what you are doing and you do not want any warnings or reminders about overwriting information. Whereas this may increasingly seem to be a good suggestion, there’s at all times a chance for errors and you may lose the info current in present information. That is why it isn’t good programming to make use of this swap parameter and when you’ve got to take action, train warning.
Writing knowledge into new information
Most occasions, we do not want an empty file because it makes little sense. Moderately, you’d desire a file with knowledge and ideally with an choice so as to add extra content material to it commonly.
The excellent news is PowerShell has a built-in command referred to as Out-File to create a file and write knowledge to it on the time of creation, offered you already know what ought to go in.
$content material = 'Good day world! That is the primary line of my file.' | out-file -filepath C:temptestfile1.txt
It will create a file referred to as textfile1.txt within the C:Temp listing and it’ll include your message.
Thus, these are the other ways to create a file together with the issues you need to consider.
Subsequent, we’ll transfer on to creating folders or directories.
Making a folder
There are 4 methods of making a folder or listing in PowerShell and the selection will depend on your programming wants.
Utilizing native Home windows PowerShell
Most likely the best strategy to create a listing is to make use of the native Home windows PowerShell command. We are able to use the identical command utilized in file creation for folder creation too, and as you might have guessed, simply change the worth of ItemType:
New-Merchandise -Path 'SharedTestFolder' - ItemType Listing
This command returns an object referred to as DirectoryInfo that has the folder mode, final written time, and the size of the identify.
You’ll be able to even skip the Path parameter and might instantly specify the trail identify and the end result would be the similar.
New-Merchandise 'SharedTestFolder' - ItemType Listing
Be sure that to incorporate the ItemType worth.
Utilizing the .NET framework class
The subsequent choice is to create a folder utilizing the system.io namespace and the CreateDirectory static technique within the Listing class.
The command you should use is:
A phrase of warning right here. It is a .NET framework class and it isn’t thought of apply to make use of it inside the Home windows PowerShell surroundings. That mentioned, the selection is as much as you and this command will create a folder. Like the sooner choice, it is going to return a DirectoryInfo object with all particulars in regards to the newly created folder.
The third technique is to make use of the Object technique from the Scripting.FileSystemObject. Word that this is identical technique utilized in VBScript and the benefits are that it’s quick and comparatively straightforward to make use of to create a listing.
$newdirectory = new-object -ComObject scripting.filesystemobject $newdirectory.CreateFolder(“SharedTestFolder”)
This command will return an object that comprises the trail and different info of the listing.
Utilizing md operate
The final choice is to create a listing utilizing the md operate. Many individuals discover this handy as they’ve used it lots within the command immediate to create numerous folders. Additionally, the syntax is pretty easy and you do not have to say the ItemType since md alone is sufficient to inform the system that you just’re making a folder.
So, the command is:
This command additionally returns a DirectoryInfo object.
Thus, these are among the other ways to create a folder in PowerShell. So, which of those 4 strategies must you use to create a folder? Nicely, it will depend on what works finest for you. For instance, if you happen to’ve been utilizing the command immediate to create folders, the md technique might be the best. However, if you wish to maintain all of the code as PowerShell instructions, utilizing ItemType is your best option. The opposite two additionally work nicely in numerous contexts. So, the selection is completely as much as you and it’ll haven’t any influence in any respect within the folder creation course of.
We hope this piece has clarified your doubts in creating information and folders in PowerShell.
Featured picture: Pixabay
Extra PowerShell Fundamentals articles