Tag Archivio per: cmdlet


PowerShell is a well-known scripting language supported by all modern operating systems. PowerShell has employed various operations to deal with data. While dealing with data at a small level, you would have observed the data is stored in an excel spreadsheet.

The Import-CSV cmdlet allows you to import the data from a CSV file to any other file or to the PowerShell console. The purpose of this article is to explain the working and usage of the Import-CSV cmdlet in PowerShell. The learning outcomes of this post are listed below:

    • How Does the Import-CSV cmdlet Work?
    • How to Use the Import-CSV cmdlet?

How Does the Import-CSV Cmdlet Work?

Basically, Import-CSV retrieves the information that is presented in separate files and generates a reference list or table. After importing the data, the foreach cmdlet is used to perform functions by iteratively extracting the content of the data in a horizontal manner. The syntax which is used to import CSV files into PowerShell is as follows:

Import-CSV <-Path> <-Delimiter> <-Encoding> <-Header> <-LiteralPath> <CommonParameters>

 
In PowerShell, each parameter has its own importance, which is written in the above syntax and is explained below with a short description.

    • Import-CSV: Creates an object having the property of a table that extracts the items present in the CSV file.
    • Path: Provides the location of the CSV file located in the system.
    • Delimiter: Specify the character to separate the values in the file, such as semicolons (;).
    • Encoding: Provides the encoding features of CSV files such as ASCII, UTF8, etc.
    • Header: Represents the custom header, which is also known as the property name.
    • LiteralPath: This path parameter works same as the path parameter. However, it excludes the wild card characters (if any).
    • CommonParameters: These parameters can be used with all the cmdlets in PowerShell such as Debug ErrorAction, Verbose, WarningAction and so on.

How to Use the Import-CSV Cmdlet?

The Import-CSV is a powerful utility to manage the data in a CSV file. In this section, we have presented a set of examples to demonstrate the usage of the Import-CSV cmdlet in PowerShell.

Note: As a reference, we are using a test.csv file from which we will show how data is handled using the Import-CSV cmdlet. The test.csv file contains the following data:

Example 1: Import All the Data Using Import-CSV

In this example, we are importing all the data stored inside an excel spreadsheet. If you want to import a CSV file, you need to specify its absolute path in the Import-CSV cmdlet. The ft keyword is used as an alternative method to store data in a table. In our case, the following command will help retrieve data from a test.csv file:

Import-CSV -Path C:UsersDELLDesktoptest.csv | ft

 

The output shows that the data extracted from a file is printed on the PowerShell terminal in a tabular form using the ft keyword.

Example 2: Extract the Specific Data Using Import-CSV

With the help of the Import-CSV cmdlet, you can print or import specific data. For instance, if there are multiple columns in an excel sheet and you want to get only one of them. You can do it by utilizing the Import-CSV cmdlet.

As an example, the following Import-CSV cmdlet retrieves the data from the file and stores it in a variable. After that, only the Age column from the retrieved data will be printed:

$users = Import-Csv -Path C:UsersDELLDesktoptest.csv
$users.Age

 

The output shows that only the $Age data is printed on the console.

Example 3: Extract Multiple Columns From a CSV File

In this method, we have extracted multiple properties of data using the Import-CSV cmdlet in PowerShell. An example command is provided below.

Write-Host «IMPORTING A CSV FILE IN POWERSHELL»
$file= Import-Csv -Path «C:UsersDELLDesktoptest.csv»
Write-Host «THE FILE IS IMPORTED USING CMDLET»
write-host «THE LIST OF SALARY IN THE CSV FILE»
$file.Salary
write-host «THE LIST OF AGE IN THE CSV FILE»
$file.Age

 
In the code, first specify the location of the file that is being imported into PowerShell. After that, extract the properties of Salary e Age using the object $file.


The outcome of the code shows that two properties, Salary, and Age, are extracted from the file and printed on the console.

Conclusion

The Import-CSV cmdlet retrieves the data from a CSV file. You can either go for the whole data retrieval, or you can also get the specific data. This post demonstrates the working and usage of the Import-CSV cmdlet in PowerShell. In this post, the working and usage of the Import-CSV cmdlet are explained in detail.



Source link


PowerShell is a Windows built-in command-line application used for task automation and scripting purposes. It is used to perform administrative tasks with the help of a large set of cmdlets, functions, and aliases.

The PowerShell Remove-Item cmdlet is used to remove files from the system via PowerShell. You can use the file names, extensions of the files, or some file name hint to trace and remove files via PowerShell.

This write-up has covered thoroughly all the concepts of the Remove-Item cmdlet.

How to use the PowerShell Remove-Item cmdlet?

The Remove-Item cmdlet in PowerShell deletes one or more than one item from the specific storage location. It uses the file path for the purpose of deletion of the item. You can delete almost everything such as files, folders, variables, etc.

Syntax

> Remove-Item [-Path] pathoffile [-Parameters]

The path of the file will be provided after the -Path flag. Moreover, a variety of standard parameters can be passed such as Force, Include, and Exclude whose usage is also described in the article.

Example 1: How to delete a single file using PowerShell?

To delete a single file from the specific path we will give the exact path of the file by using the “-Path” parameter to “Remove-Item”. The below example will give a demonstration of how to delete a single file in PowerShell.

> Remove-Item -Path C:NewDocs.txt

The above line of code will only delete a single file specified by the path you have given.

Example 2: How to delete all files of a directory using PowerShell?

The Remove-Item cmdlet will utilize the wild card “*” to delete all the files located in the folder. To do so, you need to put the “*” in place of the file name and the extension.

The execution of the below command will result in the deletion of all the files in the folder path.

Example 3: How to delete specific files using PowerShell?

The parameter “-Include” is used by the “Remove-Item” cmdlet to include only the specific files in the deletion operation. The execution of the following command will result in the deletion of the files having a specific extension (.txt). The “*” wildcard will be placed at the end of the specified folder path and also before the .txt extension to remove all the “.txt” files from the directory:

> Remove-Item -Path C:New* -Include *.txt

The above line of code will only delete only those file extensions specified by you using PowerShell.

Moreover, you can use the “-Exclude” parameter used by the “Remove-Item” cmdlet to delete the files except for the selected file extensions.

Example 4: How to delete hidden/read-only files using PowerShell?

If you want to delete hidden and read-only files then use the command below. The path of the file is given by the “-Path” parameter and the “-Force” parameter helps to remove the hidden/read-only files.

> Remove-Item -Path C:Newhidden-text-file.txt -Force

The above command has deleted all hidden and read-only files using PowerShell.

Here you go, you have learned the functionality of the Remove-Item cmdlet in PowerShell.

Conclusion

The PowerShell Remove-Item cmdlet is mainly used to remove or delete specific files and folders from a file path or location. The Remove-Item cmdlet is utilized to remove unwanted files or folders from the system. This article has covered all the aspects related to the Remove-Item cmdlet. The Remove-Item cmdlet supports a variety of parameters to remove files as per your requirement.



Source link