Category Archives: powershell

TicTacToe in Powershell

So for fun I thought I would create a two player game of tictactoe using Powershell.  It was a good excuse to mess with a multidimensional array, as you will see below.  The variable for the multidimensional array is called $ticTacMatrix, it is how I set up the grid for the game.

$ticTacMatrix+=,@(' ',' ',' ');
$ticTacMatrix+=,@(' ',' ',' ');
$ticTacMatrix+=,@(' ',' ',' ');
$gridCount = 9;

function playGame(){
    ##This function populates the matrix
    $gridPlace1 = Read-Host "Position 1";
    $gridPlace2 = Read-Host "Position 2";
    if($placeValue -notmatch "[X,O]"){
        Write-Output "invalid";
    }elseif([int]$gridPlace1 -gt 2 -or [int]$gridPlace2 -gt 2){
        Write-Output "outofrange";
    }elseif($ticTacMatrix[$gridPlace1][$gridPlace2] -match "[X,O]"){
        Write-Output "inuse";
    } else {
        $ticTacMatrix[$gridPlace1][$gridPlace2] = $placeValue;

function checkwin(){
    ##This function checks to see if there is a winner
    $winner = '';
    $firstColumn = $gameBoard[0][0],$gameBoard[1][0],$gameBoard[2][0];
    $secondColumn = $gameBoard[0][1],$gameBoard[1][1],$gameBoard[2][1];
    $thirdColumn = $gameBoard[0][2],$gameBoard[1][2],$gameBoard[2][2];

    $firstDiagonal = $gameBoard[0][0],$gameBoard[1][1],$gameBoard[2][2];
    $secondDiagonal = $gameBoard[0][2],$gameBoard[1][1],$gameBoard[2][0];

    if(($gameBoard[0] -match 'X').count -eq 3){$winner='X';}
    if(($gameBoard[1] -match 'X').count -eq 3){$winner='X';}
    if(($gameBoard[2] -match 'X').count -eq 3){$winner='X';}
    if(($firstColumn -match 'X').count -eq 3){$winner='X';}
    if(($secondColumn -match 'X').count -eq 3){$winner='X';}
    if(($thirdColumn -match 'X').count -eq 3){$winner='X';}
    if(($firstDiagonal -match 'X').count -eq 3){$winner='X';}
    if(($secondDiagonal -match 'X').count -eq 3){$winner='X';}

    if(($gameBoard[0] -match 'O').count -eq 3){$winner='O';}
    if(($gameBoard[1] -match 'O').count -eq 3){$winner='O';}
    if(($gameBoard[2] -match 'O').count -eq 3){$winner='O';}
    if(($firstColumn -match 'O').count -eq 3){$winner='O';}
    if(($secondColumn -match 'O').count -eq 3){$winner='O';}
    if(($thirdColumn -match 'O').count -eq 3){$winner='O';}
    if(($firstDiagonal -match 'O').count -eq 3){$winner='O';}
    if(($secondDiagonal -match 'O').count -eq 3){$winner='O';}

    if($winner -eq 'X' -or $winner -eq 'O'){
        Write-Output $winner;

Write-Host "Welcome to Powershell based TicTacToe" -ForegroundColor 'Green';
Write-Host "Choose your position based on cordinates, 0,0 is the top left corner." -ForegroundColor 'Green';
Write-Host '';

Foreach($row in $ticTacMatrix){$row -join '|';}

$inputVal = 1;
$everyOther = 'no';
$player = 'X';
while($inputVal -le $gridCount){
    Write-Host '';
    Write-Host "Player: $player" -ForegroundColor 'Cyan';
    $gamePlayValue = playGame -placeValue $player;
    if($gamePlayValue -eq 'inuse'){
        Write-Host "Already in use" -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
    }elseif($gamePlayValue -eq 'outofrange'){
        Write-Host "Position out of range" -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
    }elseif($gamePlayValue -eq 'invalid'){
        Write-Host 'Invalid player' -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
    Write-Host "Choose your position based on cordinates, 0,0 is the top left corner." -ForegroundColor 'Green';
    Write-Host '';
    Foreach($row in $ticTacMatrix){$row -join '|';}
    $gameOver = checkWin -gameBoard $ticTacMatrix;
    if($gameOver -eq 'X'){
        Write-Host '';
        Write-Host 'X Wins' -ForegroundColor 'Green';
    if($gameOver -eq 'O'){
        Write-Host '';
        Write-Host 'O Wins' -ForegroundColor 'Green';
    if($everyOther -eq 'no'){
        $player = 'O';
        $everyOther = 'yes';
        $player = 'X';
        $everyOther = 'no';

if($inputVal -eq 10){
    Write-Host "No Winners" -ForegroundColor 'Red';



PowerShell and Roku API

Below is a simple one liner to send a command to your Roku via PowerShell command.

Invoke-WebRequest -uri '' -Method POST -Body ""

Pull AD Group Members and Member Properties

##Pull Active Directory Group Members, and Member Properties
##Get AD Groups via text file or Get-ADGroup command
$adGroups = Get-Content “C:\Temp\adGroups.txt”;
#$adGroups = Get-ADGroup -Filter *;

Foreach ( $group in $adGroups ) {
     $groupName = $group;
     #$groupName = $group.Name;
     $adGroupMembers = Get-ADGroupMember $groupName;

     Foreach ( $member in $adGroupMembers ) {
          $memberSamAcct = $member.SamAccountName;
          $adUser = Get-ADUser $memberName;
          ##Get other AD User attributes here
          echo “$groupName,$memberSamAcct”;

PowerShell Compare User Info in Multiple Domains

Below is a script to compare a user’s Given Name between two domains.  Useful for a development and production comparison.

##Get Domain Credentials
$domain1Creds = Get-Credential ##Enter credentials for first domain;
$domain2Creds = Get-Credential ##Enter credentials for first domain;

##Get Domain Users
$adUsersD1 = Get-ADUser -Filter * -Server Domain1 -Credential $domain1Creds;
$adUsersD2 = Get-ADUser -Filter * -Server Domain2 -Credential $domain2Creds;

Foreach ( $user in $adUsersD1 ) {
     $userName = $user.SamAccountName;
     $userGivenName = $user.GivenName;

     ##Get Index and entry of user in the adUsersD2 array
     $d2Index = $adUsersD2.SamAccountname.IndexOf($userName);
     $user2Compare = $adUsersD2[$d2Index];

     ##Compare Given name from domain1 to domain2
     if ( $userGivenName -ne $user2Compare.GivenName ) {
          echo “$userName Given name is different in domain2”;

Validate Web Site is Up – Powershell

##Create WebClient
$webClient = New-Object System.Net.WebClient;
try {
     ##Download site source code
     $source = $webClient.DownloadString(“webURL”);
} catch {
     ##Couldn’t connect
     echo “Down!”;

PowerShell and Regular Expressions

Here is a simple example of finding matches in a line of text using Regular Expressions.

In the example below I am searching for the word “test” in the variable named $myVar.

The command $myRegEx = [regex]”test” defines what we are looking for and sets the variable to a regex type.

The third command searches through $myVar for the word test and the next command shows count of findings.


WebPart Targeting with Powershell

WebPart Targeting with Powershell in SharePoint 2013

$web = Get-SPWeb "http://sharepoint/Page2";
$sharepointGroups = ';;Group1,Group2';
$webPath = "pages/yourpage.aspx";
$webPM = $web.GetLimitedWebPartManager($webPath, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$webParts = $webPM.WebParts;
$myWebPart = $webParts | where { $_.Title -match "Web Part name" };
$myWebPart.AuthorizationFilter = [string]"$sharepointGroups";