Posts

Showing posts from January, 2016

Get-ChildItem -Include and -exclude with multiple file extensions

Image
On day I was writing a PowerShell script to streamline my photography workflow. I would like to copy the photo files including the RAWs from memory card to the hard drive, and put them into the the folders named by the date of shooting. I decided to filter early in the Get-ChildItem so it only checks the photo related files* and skip others. The Get-ChildItem has the -Include syntax suit the purpose well. However, there was a tiny rock in the way: getting the multiple file extensions work with the -include parameter. In the shell, following worked fine: But when the '*.jpg, *.cr2' was saved in to a variable, $list, for the script, Get-ChildItem returned nothing. Saving them into an array worked for the script. See the list below. Well, the full help of Get-ChildItem did tell me that it wants String[] for the -include parameter. But what interested me was why? I then ran the tracing in each scenarios. Please have a look of each screenshot below. In scenari

Getting the help descriptions in Group Policy settings with PowerShell

Image
As a windows administrator, I deal with group policy frequently. From time to time, I forget what a specific setting mean. I'm glad that Microsoft is including the help/ description with the group policy templates so I can view details by opening up the setting in GPMC. See the highlighted section in the screenshot: That's a good and easy when checking only few settings and only me, myself and I are involved. Chances are, I have to review and discuss the group policy settings with others on my team. (Or the security people. You know they love these 'controls'...) In the meeting, the scrolling and clicking within GPMC seems to easily nullify the caffeine in everyone's brain within five minutes. Sometimes I wish there is a simple group policy report that shows the configured settings AND their descriptions so I can email it to people before the meeting! Well, I did try once to copy-paste the content from Microsoft's Group Policy Reference . But I just felt

Windows Version Number

Here is a list of Windows version number that can be used in WMI query/ filtering within Group Policy. Or they can be used together with Get-WmiObject/ Get-CimInstance cmdlet when needed. For example, you can perform a quick scan for a list of computer whether from AD or other sources. Operating system Version number Windows 10 10.0* Windows Server 2016 Technical Preview 10.0* Windows 8.1 6.3* Windows Server 2012 R2 6.3* Windows 8 6.2 Windows Server 2012 6.2 Windows 7 6.1 Windows Server 2008 R2 6.1 Windows Server 2008 6 Windows Vista 6 Windows Server 2003 R2 5.2 Windows Server 2003 5.2 Windows XP 64-Bit Edition 5.2 Windows XP 5.1 Windows 2000 5 Source: MSDN – Operating System Version

Learning PowerShell as a Windows Admin

Image
Today I would like to share the PowerShell learning resources that have been tremendously helpful to me as an IT Pro/ Windows Admin. Hope you will find some of these useful to you as well. This post will be updated as I continue learning. Before jumping into the resources, allow me to share some thoughts with you. I started using the PowerShell v3 as a replacement for good old faithful command line and the gui. Honestly, the change wasn't that smooth during the first few weeks. I had to refer to the Get-Help command and Google a lot even for a recursive directory copy. You might ask now "That sounds... a lot of work. Does it really worth the time learning?" True, things already work (and work fine) in the cmd and gui world. You know how to finish a task in somewhat reasonable time. However, how often do you want to share an exciting solution with your team and ended not doing it because you just have no appetite to capture all the 17 screenshots, highlight the 'NE

ValidateCount and pipeline

Image
One day I used the ValidateCount() in a PowerShell script for simple throttling. (List 1.) When testing, I found the script didn't throw the expected red stuffs and actually accepted more piped objects than specified in the ValidateCount(). See figure below. It took all six arguments when ValidateCount(0,5) set to accept maximum 5. Moments after, I realized that the pipe passes one object at a time so the validation would always return true. The $_ , that's one object and one object only. Or have a look of the debug result showen. Figure below shows the ValidateCount() works as expected when using the parameter.The kind of validation will have to also happen within the script/ advanced function when values are expected from the pipeline. List 1.

PowerShell ISE: A couple useful keys

Image
When I started learning PowerShell a couple years ago, I was annoyed a bit about the ISE. Mainly because I had to frequently move my hand back and forth between the keyboard and the trackball. Why? Because I was trying to validate every PowerShell command along the script and the cursor had to be move between the script pane and the console. And one day these magic key combinations showed up gloriously! (Well, I probably wasn't looking careful enough...) : Ctrl + I : move cursor to the Script Pane Ctrl + D: move cursor to the Console These look simple but they do help with my scripting that I'm less interrupted. Try them now in PowerShell ISE if you haven't already. It'll definitely help enhancing your scripting experience. Do you have any tips like this you can share? Please feel free to post a comment.

A PowerShell Pomodoro clock.

Image
Pomodoro is a time management technique I use for protecting my eyes. While  Tomighty  is a great Pomodoro tool, it doesn't work very nicely in multi-monitor set-up. Since I always have PowerShell open, it makes sense to have it do the timing for me. Things I want it to do: Have a progress bar for the Pomodoro clock (default: 25min) Have another progress bar for the break  (default: 5min) A pop-up window (and grab the focus) at the end of each period. Have option for audio feedback I had the flow kinda ready but didn't really know how how to handle the pop-up the way I wanted: a pop-up that grabs the focus. Thanks to Sonam Rastogi mentioning in the recent  Blog post  about using the VisualBasic MessageBox and the 'ShowModal' value. Here is the script module, PoshPomodoro , and you can run the Start-Pomorodo once imported. A couple screenshot here: Screenshot 1. Progress bar Screenshot 2. Pop-up And below is the script for your easy reference. What

Using PowerShell to scan for Cryptowall encrypted files

During the post clean-up in a Cryptowall incident, a challenge is to identify the files encrypted so those can have top priority. Having someone to open every single file and see which one breaks? It can be done but you don’t really want go that direction. With a good backup system in place already prior the Cryptowall incident, PowerShell is the perfect tool to do the heavy lifting to point out those affected files so you can focus the restoration effort at the right spots. The short script below is used in a previous incident to help a customer. In the case, the encrypted files had same file header, +O)T.  To speed up the scan,  Get-Content  is used with  -TotalCount 1  to retrieve only the beginning of the files. PowerShell is the perfect tool to do this. Please note that the pattern could be different among different variants of Cryptowall or CryptoLocker. But you can gather the pattern once you unwillingly have the encrypted files. $path = "C:\" #Path to scan