CSV stands for “Comma Separated Value,” and it is an open file customized within the spreadsheets, such as MS Excel or Google Sheets. The data inside a “CSV” file is saved in tabular format. With a bunch of other features, PowerShell enables the user to import data into an array. PowerShell uses the “Import-CSV” cmdlet in order to import data from a file into an array.
This tutorial will demonstrate a guide to import data into an array.
How to Import Data Properly into an Array Using PowerShell?
The data can be imported into an array using PowerShell’s “Import-CSV” cmdlet. After importing the data into an array, the index number can be used to access it.
Before importing data from a “CSV” file, let’s first view the data in raw format. For that reason, use the “Get-Content” cmdlet to retrieve the data of the file specified by the user:
The raw data has been displayed in the output separated by commas.
Now, let’s import the data from a file into an array:
> $File
According to the given command:
- First of all, use the “Import-CSV” cmdlet and then assign the “CSV” file path and store it to the variable.
- After that, call the variable by its name to view the imported data:
It can be observed that the data has been imported into an array, and it has displayed the data in tabular form.
Verification
Let’s verify whether or not the data has been imported or not into an array or not by executing the given code:
Here, we have invoked the variable in which the data is stored and called the value stored at the “0” index:
The value from an array has been fetched successfully.
Conclusion
To import data into an array, PowerShell uses the “Import-CSV” cmdlet. The “Import-CSV” cmdlet not only imports the data but also formats the data into a tabular form by removing the commas between values. Moreover, the values of the CSV file can be displayed in the output by calling them with their index number. This post has provided a thorough guide to import data into an array.