My First PowerShell Script - Moving Files From One Directory to Another

Microsoft’s new command-line tool, PowerShell has been out for quite a few years now and I thought today will be the day I would start using it. I needed to write a script that would move n number of files from one directory to another. This job seemed a perfect fit for PowerShell.

#Get 'n' number of files
$FileLimit = 10 

#Destination for files
$DropDirectory = "C:\Drop\"

$PickupDirectory = Get-ChildItem -Path "C:\Pickup\"

$Counter = 0
foreach ($file in $PickupDirectory)
{
    if ($Counter -ne $FileLimit)
    {
        $Destination = $DropDirectory+$file.Name

        Write-Host $file.FullName #Output file fullname to screen
        Write-Host $Destination   #Output Full Destination path to screen
        
        Move-Item $file.FullName -destination $Destination
        $Counter++
    }  
}

From the get go, I was really impressed with the flexibility of the scripting language. This is where command line fails. It is sufficient for simple tasks but not so much for complex jobs.

As you can see from the code above, I can implement complex operations that supports variables, conditional statements, loops (while, do, for, and foreach), and that’s just the start. I don’t know why I hadn’t used PowerShell sooner. If I didn’t have the option to use PowerShell, I would have probably created a C# service or executable to do the exact same thing. Time saver!

Since PowerShell is built on the .NET Framework, Windows PowerShell helps control and automate the administration of the operating system and applications that run on Windows. So if you are a C# programmer, you should feel comfortable in writing PowerShell scripts. All you need to be aware of is syntax differences when declaring variables and keywords.

To end with, I will quote an amusing forum post I found when researching the difference between good ol’ Command Prompt and PowerShell:

PowerShell has a default blue background and Command Prompt has a default black background.”

Beginner’s Guide To Using Google Plus .NET API Part 1: Profile Data

Google has always impressed me with the quality of their API libraries allowing us to interface with their products in a somewhat straight-forward manner. In the past, I’ve used a couple of Google’s API’s for implementing YouTube video’s or Checkout merchant within my own sites. What makes life even easier is that the API’s are available in my native programming framework - .NET.

Google were quite slow in launching an official API upon Google Plus’s initial release and even though unofficial API’s were available, I thought it would be best to wait until an official release was made. I’ve been playing around with Google’s .NET API for a couple weeks now and only just had the time to blog about it.

I am hoping to make this beginners guide a three part series:

  1. Profile Data
  2. User Posts
  3. User’s +1’s

So let’s get to it!

Today, I shall be showing you the basic API principles to get you started in retrieving data from your own Google+ profile.

Prerequisites

Before we can start thinking about coding our page to retrieve profile information, it’s a requirement to register your application by going to: https://code.google.com/apis/console. Providing you already have an account with Google (and who hasn’t?), this shouldn’t be a problem. If you don’t see the page (below), a new API Project needs to be created.

Google Plus API - Console

Only the Client ID, Client Secret and API Key will be used in our code allowing us to carry our API requests from our custom application.

Next, download the Google Plus .NET Client. My own preference is to use the Binary release containing compiled .NET Google Client API and all dll's for all supported services.

Building A Custom Profile Page

1) Create a new Visual Studio Web Application.

2) Unzip the Binary Zip file containing all Google service DLL’s. Find and reference the following DLL’s in your project:

  • Google.Apis.dll
  • Google.Apis.Authentication.OAuth2.dll
  • Google.Apis.Plus.v1.dll

3) Copy and paste the following front-end HTML code:

<h2>
    About Me
</h2>
<br />
<table>
    <tr>
        <td valign="top">
            <asp:Image ID="ProfileImage" runat="server"></asp:Image>
        </td>
        <td valign="top">
            <strong>Name:</strong> <asp:Label ID="DisplayName" runat="server"></asp:Label>
            <br /><br />
            <strong>About Me:</strong> <asp:Label ID="AboutMe" runat="server"></asp:Label>
            <br />
            <strong>Gender:</strong> <asp:Label ID="Gender" runat="server"></asp:Label>
            <br /><br />
            <strong>Education/Employment:</strong> <asp:Literal ID="Work" runat="server"></asp:Literal>
        </td>
    </tr>
    <tr>
        <td colspan="2" valign="middle">
            <asp:HyperLink ID="GotoProfileButton" runat="server">Go to my Google+ profile</asp:HyperLink>
        </td>
    </tr>
</table>

4) Copy and paste the following C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Plus.v1;
using Google.Apis.Plus.v1.Data;

namespace GooglePlusAPITest
{
    public partial class About : System.Web.UI.Page
    {
        private string ProfileID = "100405991313749888253"; // My public Profile ID
        private string GoogleIdentifier = "<GoogleIdentifier>";
        private string GoogleSecret = "<GoogleSecret>";
        private string GoogleKey = "<GoogleKey>";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                GetGooglePlusProfile();
        }

        private void GetGooglePlusProfile()
        {
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = GoogleIdentifier;
            provider.ClientSecret = GoogleSecret;

            var service = new PlusService();
            service.Key = GoogleKey;

            var profile = service.People.Get(ProfileID).Fetch();

            // Profile Name
            DisplayName.Text = profile.DisplayName;

            //About me
            AboutMe.Text = profile.AboutMe;

            //Gender
            Gender.Text = profile.Gender;

            // Profile Image
            ProfileImage.ImageUrl = profile.Image.Url;

            // Education/Employment
            StringBuilder workHTML = new StringBuilder();

            workHTML.Append("<ul>");

            foreach (Person.OrganizationsData work in profile.Organizations.ToList())
            {
                workHTML.AppendFormat("<li>{0} ({1})", work.Title, work.Name);
            }

            workHTML.Append("</ul>");

            Work.Text = workHTML.ToString();

            //Link to Google+ profile
            GotoProfileButton.NavigateUrl = profile.Url;            
        }
    }
}

Once completed, the page should resemble something like this:

Google Plus Profile Page

I think you can all agree this example was pretty straight-forward. We are simply using the people.get method which translates into the following HTTP request:

https://www.googleapis.com/plus/v1/people/100405991313749888253?key=APIKey

Unless you really want to display my profile information on your site (who wouldn’t!), you can keep the code as it is. But you have the flexibility to change the “ProfileID” variable to an ID of your own choice. To find your Profile ID, read: How Do I Find My Google Plus User ID?.