Import CSV files in Powershell

If you work a lot with powershell and different Microsoft products then you will run into importing CSV files. Importing CSV files, can be a cumbersome task in Powershell. So here are a few tips and tricks. This article is a kind of two-fer in that we’ll look at working with CSV files and resolving computer names to IP addresses.

To begin, I have a sample CSV file.

The great thing about PowerShell is that you can bring any CSV into PowerShell using Import-CSV. The cmdlet will take the column heading and use it as property names.

Importing a CSV file into PowerShell (Image Credit: Jeff Hicks)

I created my CSV file so that some of the headings match cmdlet parameters I might want to use. Let me explain. One technique I could use to get an IPAddress s with the Test-Connection cmdlet.

Using PowerShell's Test-Connection cmdlet to see an IP address (Image Credit: Jeff Hicks)

How does this relate to my CSV file? This is when you need to look at cmdlet help, specifically in this case at the Computername parameter.

The Test-Connection Computername parameter (Image Credit: Jeff Hicks)

The description says it can be a name. Good, because that is what I have in the CSV file. But here’s the cool part. Take a look at the ‘Accept pipeline input’ section. It has a value of True and an indication of ByPropertyName. This means the cmdlet will accept piped in objects. If the incoming object has a property name called Computername, PowerShell will take that value and bind it to the corresponding parameter. Let me do a quick proof of concept.

Testing parameter binding (Image Credit: Jeff Hicks)

If you recall each imported object has a Computername property courtesy of the Computername column heading. Test-Connection sees it and hooks everything up for me. Let’s try it now for the entire CSV file.

Testing connections from imported objects (Image Credit: Jeff Hicks)

Errors aside for offline or unknown computers, this works!

Before we get to the next step, what do you do if your CSV file doesn’t have a matching heading that you can use with parameter binding? The answer is not to edit the file. This is a situation where you will need to use ForEach-Object. Import the CSV and for each imported object, do something.

It works, but is a bit more cumbersome to type. If you have a process planned for your CSV that involves PowerShell cmdlets, the more you can setup your CSV ahead of time with the right column headings, the easier the import and processing.

If your CSV doesn’t have a header line, you can specify one at import:

Or, let’s say your CSV does have a header but you want to use something different and can’t really modify the source file. You can’t use Import-CSV with the Header parameter. Instead, you have to strip off the first line from the file.

Stripping off the header line from a CSV

To bring this into PowerShell as a set of objects, use the ConvertFrom-CSV cmdlet, which also has Header parameter.

Converting a CSV with an alternate header

Since my file is already using Computername, I used Name for the sake of demonstration. From here I could pipe to another cmdlet and take advantage of pipeline binding.

Fortunately, my CSV file is structured, so I don’t have to use any of these tricks.

Remove Multiple Modern Apps from Windows 8

There are numerous modern apps that are a built into Windows 8 and they are featured prominently on the Start screen. It may well be the case that you don’t want to use them because they just aren’t apps you’re interested in, or it may be that you have found a better alternative.

You can right click a tile and selecting the uninstall option, but if you want to remove multiple apps at once you can do so with a PowerShell Script. You can also use this technique to uninstall multiple apps you have installed from the Store.

You can download a readymade script from the TechNet Script Center that can be used to select all of the modern apps you want to remove, and uninstall them all at once. Download the script and extract it from the zip file.

Launch PowerShell in Administrator mode by pressing the Windows key to bring up the Start screen and then type Powershell. Right click the Windows PowerShell icon and then click the ‘Run as administrator’ button at the bottom of the screen before clicking Yes in the User Account Control dialog.

Run the script by typing its full path at the command prompt and then press Enter.

You will see a list of all of the modern apps you have installed. This list includes apps that you have installed and those that are built into Windows 8.

Each app has a number next to it and you can use these to specify which apps should be uninstalled. Just enter all of the app numbers, separated by commas, and press Enter.

Confirm the action and after a moment or two the selected apps will be removed for you.

Should you change your mind about an app you have removed, you can easily reinstate it from the Store.

It may be that you would like to remove all modern apps form your hard drive, and if this is the case you have a couple of options available to you.

The first option can be used to remove all apps from the account you are currently logged into.  Launch PowerShell as an administrator and type the following command before pressing Enter:

Secondly, you can use the following command to remove all modern apps from all user accounts:

Manage Windows Optional Features From PowerShell

The first thing you will want to do is see what features you have enabled, to do this we will need to pass the output of the Get-WindowsOptionalFeature cmdlet down the pipeline, where it can be filtered and formatted:

That will give you a nice tabulated view of what is enabled.

If you want to see what features are disabled you can use the following:

 

If you need to disable a feature you can use the following:

This assumes that the feature that you want to disable is NetFx3.

Of course, you will most likely be adding a feature which can be done like so:

That’s all there is to it.

PowerShell Quick Reference Guides from Microsoft

Are you just getting started with learning PowerShell or tired of looking up less frequently used commands? Then this terrific set of PowerShell quick reference guides from Microsoft is just what you need!

The first guide focuses on commonly-used Windows PowerShell commands and is available in a single .doc format document. The other guides are available as a set (six files) in .pdf format and focus on: tips, shortcuts, and common operations in Windows PowerShell 3.0, Windows PowerShell Workflow, Windows PowerShell ISE, Windows PowerShell Web Access, Server Manager for Windows Server 2012, WinRM, WMI, and WS-Man.

Keep in mind that you can select all the guides or just the ones you need to download for the PowerShell 3.0 set.

Windows PowerShell Quick Reference [Microsoft]

Windows PowerShell 3.0 and Server Manager Quick Reference Guides [Microsoft]

[via The Windows Club here and here]

Windows Server 2012 (R2) – Turn GUI off or on

Windows Server 2012 (R2) can be migrated with a single reboot to a Server Core and back. This quite handy feature is mostly used on domain controllers or Hyper-V controllers, this way you can install and configure your server roles with an GUI interface and afterwards go back to the Core edition to require lower recourses and have a small attack surface. These commands only work if you have installed the server with the GUI, you can’t install the GUI if you have installed the core edition.

If you want more information about the Windows Server Core features: please visit this link http://technet.microsoft.com/en-us/library/dd184075.aspx 

You can turn off the GUI with the following PowerShell command:

Or turn the GUI back on if you want to modify something on your server with the following PowerShell command:

Don’t forget to reboot after running these commands succesfully.

This can be done with the following command: