Quicktip: Use a Csv file to splat parameters into a function

I recently received a question on Reddit that asked if it is possible to use a csv file to create a hash table that can be used for splatting parameters into a function. The goal of this exercise is to be able to specify a csv file and to pass on the proper arguments to a function.

For example there is a csv file with the following contents:

SplatCsv

So based on this the New-Server function should be called as such:

1
New-Server -Templare 2012R2 -Spec DomainJoinSpec -Datastore Production2 -Cpus 1

In order to achieve this, the first step would be to remove the empty fields from the equasion as such:

1
2
$Csv = Import-Csv -Path Parameters.csv
$Csv.psobject.Properties | Where-Object {$_.Value}

This will import the csv and only display the colums in the csv that actually contain data. To ensure this data is stored in a hash table we can use the ForEach-Object. This cmdlet can be used to loop through the remaining results and to create a hashtable:

1
2
3
4
5
6
$Csv = Import-Csv c:\temp\params.csv
$Csv.psobject.Properties | Where-Object {$_.Value} | ForEach-Object -Begin {
$SplatParams = @{}
} -Process {
$SplatParams[$_.Name] = $_.Value
}

Now that the hash table has been created this can be used for splatting into the New-Server function:

1
New-Server @SplatParams

By combining the ForEach-Object cmdlet and the PSObject property that is piped into Where-Object it is possible to construct a custom hashtable that can be used for splatting. This can provide an easy alternative for non-technical users to provide parameters and arguments into a PowerShell function.

TwitterLinkedInFacebookGoogle+RedditWordPressEmailTumblrPinterestHacker NewsShare

New script Remove-ScheduledTask in TechNet Script Gallery

One of the things that keeps me busy is maintaining the library of scripts I have made available in the TechNet Script Library. Earlier this week I noticed a request for my Get-ScheduledTask script, a script that gets a list containing the scheduled tasks from either the local machine or a remote system. The request stated if it was possible to create a function that could delete scheduled tasks.

Based on this question I had a look at the Get-ScheduledTask script and decided that it requried an additional property in the custom object that it outputs, the computer name. Using both the computer name and the path of a scheduled task I am able to delete scheduled tasks on both local systems as well as remotely.

Here is an example of Remove-ScheduledTask in combination with Get-ScheduledTask:

Get-ScheduledTask-Remove

.EXAMPLE
.\Get-ScheduledTask.ps1 | Where-Object {$_.State -eq 'Disabled'} | Remove-ScheduledTask -WhatIf

Description
-----------
Get-ScheduledTask will list all the disabled tasks on a system and the Remove-ScheduledTask function will list all the actions that could be taken

For more information or the direct download links of these scripts please refer to the links below. Feel free to leave a comment either here or in the TechNet Script Library.

TechNet Script Gallery
My entries in TechNet Script Gallery
Get-ScheduledTask
Remove-ScheduledTask

 

Video Presentation with Experts Live TV available online on Channel9 and YouTube

Last week I met up with Bert Wolters for a video presentation/interview. Bert has been recording a series of sessions on the latest and greatest functionality in Windows 10 and in this episode I discuss some of the new functionalities in PowerShell 5.0 in combination with Windows 10. The video has been recorded in Dutch, so unfortunately it will a bit hard to follow for the English speaking community.

In the video I showcase the following features:

  • Better Multiline support for the PowerShell console
  • Colored console for cmdlets, parameters and arguments
  • PowerShell cmdlets for Windows Defender
  • PowerShell debugging in the ISE using <CTRL> + B
  • DuPSUG, Dutch PowerShell User Group

Alternatively the video is also available on Microsoft’s Channel9:

For more information or the direct link of the video in this article please refer to the links below. Feel free to leave a comment either here or in the YouTube comment section.

Links in the article
Channel9 – Experts Live TV – 10 weken Windows 10 – Aflevering 9 – Powershell 5
Channel9 – Windows 10 in 10 Minuten
YouTube – Experts Live TV – 10 weken Windows 10 – Aflevering 9 – Powershell 5
DuPSUG.com, Dutch PowerShell User Group

Clear-RecycleBin New PowerShell 5.0 cmdlet, available in Windows 10

In PowerShell 5.0 a new cmdlet has been introduced that provides the ability to clear the Recycle Bin programmatically. It supports the following four parameters:

  1. DriveLetter
  2. Force
  3. WhatIf
  4. Confirm

Unfortunately there is no Get-RecycleBin cmdlet, for this purpose I have written a short function that enumerates the contents of Recycle Bin for the current user:

1
2
3
4
Function Get-RecycleBin {
    (New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |
	Select-Object Name,Size,Path
}

Get-RecycleBin

By verifying this output we can now determine if we would like to Clear the Recycle Bin or if some files that are stored there should be recovered before deleting all files. The following command will delete all files from the Recycle Bin for all drive letters:

1
Clear-RecycleBin

Clear-RecycleBin

The default mode of operation of this cmdlet is to ask for confirmation before deleting all files. This is similar behavior to right clicking the icon of Recycle Bin and clicking ‘Empty Recycle Bin’. To delete the files without confirmation the following code can be used:

1
Clear-RecycleBin -Force

Alternatively the Confirm parameter can also be explicitly set to $false:

1
Clear-RecycleBin -Confirm:$false

Unfortunately there is no online help available for this cmdlet, so for more information about this cmdlet please refer to the built-in help in PowerShell:

1
Get-Help Clear-RecycleBin -ShowWindow

New versions of Connect-Mstsc and Get-OrphanHomeFolder in TechNet Script Gallery

One of the things that keeps me busy is maintaining the library of scripts I have made available in the TechNet Script Library. I get a good number of questions and requests about my scripts there and I enjoy picking out some of the interesting or useful requests to implement them in my scripts. Recently I received some requests for new functionality in some of my scripts so I decided two of my scripts for the latest round of updates.

For Connect-Mstsc I have updated both the PowerShell 3.0 and up version as well as the version that is backwards compatible with PowerShell 2.0. A new parameter has been introduced, -Public, which corresponds with the /public switch of the mstsc.exe tool. It runs Remote Desktop in public mode, which was requested by MSFTW. Here is an example of this switch parameter in action:

.EXAMPLE   
Connect-Mstsc -ComputerName server01:3389 -User contoso\jaapbrasser -Password supersecretpw -Public

Description 
-----------     
A RDP session to server01 at port 3389 will be created using the credentials of contoso\jaapbrasser and the /public switch will be set for mstsc

Get-OrphanHomeFolder has been updated to support wildcards/regular expressions to be able to exclude folders. This came from a request of martin_i who has a lot of folders named .v2 which he would like to exclude instead of manually specifying each path. Here is an example:

.EXAMPLE    
.\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\Server02\Fileshare\Home -MoveFolderPath \\Server03\Fileshare\MovedHomeFolders -ExcludePath '\.v2$' -RegExExclude 
 
Description: 
Will list all the folders in the \\Server02\Fileshare\Home folder and will move orphaned folders using robocopy, excluding folders that end with .v2

For more information or the direct download links of these scripts please refer to the links below. Feel free to leave a comment either here or in the TechNet Script Library.

TechNet Script Gallery
My entries in TechNet Script Gallery
Script to get orphaned home folders and folder size
Connect-Mstsc – Open RDP Session with credentials
Connect-Mstsc – Open RDP Session with credentials (PowerShell 2.0)