Powershell basics part 9 – operations with files and folders


Hello everyone,
In today’s post I will talk about some of the basic operations that you can do when working with files and folders in powershell. If you have worked with command prompt before then it will be easy for you to transition to powershell. Commands that were used when interacting with files and folders are also used here in powershell or they are aliases for cmdlets so it will be very simple to learn the new stuff. We will talk about actions like creating,moving,deleting files and folders.
OK, first let’s start powershell in our root folder, mine is C:\Powershell:

Powershell

 

Now lets start by looking at the files and folders that are located here by typing Get-ChildItem (remember that you can also type ls or dir):
Get-ChildItem
 
As you can see I have several files and folders created here. I remembered that I had a file here named cmd.txt that I can’t locate right now, you can always test a path to a certain file or folder by using the cmdlet Test-Path, but first let’s type Get-Help Test-Path:
Get-Help Test-Path
Now I will test if the file that I am looking for is there or not:
                        Test-path Powershell
OK, I have probably deleted it. Now I want to move the .csv file in the folder called scripts. To do this I will use the Copy-Item cmdlet (you can always type Get-help Copy-Item):
Copy-item cmdlet
I have used the * character to indicate that I want all files with the .csv extension to be copied to “script” folder,instead I could have typed Copy-Item allcmdlets.csv C:\powershell\script to copy only that file. To move a file use the Move-Item cmdlet like in the following example:
Copy-item cmdlet
As you can see an error is returned saying that the file already exists. To overwrite a file add the –Force argument in the command:
Move-Item Powershell
To see all the Move-Items cmdlet arguments type Get-Help Move-Items -full. Now let’s rename the file called alias.txt to names.txt by using the cmdlet Rename-Item:
Get-Help Move-Items
Now the file has been renamed. If we want to delete this file we can use the cmdlet Remove-Item:
Remove-item Powershell
There are several parameters to the Remove-Item cmdlet, we can find those by typing Get-Help Remove-Item -full :
Remove-item cmdlet
All the parameters are showed and explained here in detail so it is easy to understand them. OK, the file was deleted. Now if we want to open the file called cmdlet.txt we type the following :
C:\Powershell> invoke-item .\cmdlets.txt . This command will open the file in the program that was created. To display the items inside a file in powershell type C:\Powershell> Get-Content cmdlets.txt . If the file has a lot of information you can always use the piping method to select just the items that you need in a file by using Select-String cmdlet:
Select-String cmdlet
I know that the this file contains the number 144937 so I can filter the result by selecting only this line:
Select-String Powershell
This command is very useful when for example you are trying to find a particular line in a big file like for example a log file (can be IIS,FTP,event viewer,database files etc.)
The Set-Content Add-Content cmdlets are used to alter the information contained in files. Set-Content is used to write or to replace the existing information of a file and Add-Content is used to add content to an existing file:
Add-Content cmdlet
Add-Content Powershell
To create a file we use the New-Item cmdlet:
New-Item Powershell
When using the New-Item cmdlet we have to specify the -type parameter which can be a file or a directory like in the following example:
New-Item cmdlet
To erase the contents of a file use the Clear-Content cmdlet : Clear-Content allcmdlets.txt
 
To export the contents of a file to HTML/XML or CSV we use the following cmdlets:
ConvertTo-HTML
Export-Clixml 
Export-Csv 
More about them by typing Get-Help cmdletname
To import the contents of a Csv or XML file use the following cmdlets
Import-Clixml
Import-Csv
To print the output of a command use :
Out-Printer
an example:
C:\Powershell> Get-Content cmdlets.txt | Out-Printer
 
Some of this cmdlet we have used in the powershell recap tutorial so you may recall them.
OK, I think that’s it for now, we have covered the basics of using files and folders when working with powershell, I hope you’ve enjoyed it, have a wonderful day.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s