PowerShell for Windows Admins


June 12, 2017  7:57 AM

Find the logged on user

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

One method of finding the logged on users is to use CIM

$ComputerName = $env:COMPUTERNAME
Get-CimInstance -ClassName Win32_Process -ComputerName $ComputerName -Filter "Name = 'explorer.exe'" | 
foreach {
    
    $lguser = Invoke-CimMethod -InputObject $psitem -MethodName GetOwner
    
    $Properties = @{
       ComputerName = $ComputerName
       User = $lguser.User
       Domain = $lguser.Domain
       Time = $User.CreationDate 
    }
          
    New-Object -TypeName PSObject -Property $Properties
 }

Get the Win32_Process instances for explorer.exe and foreach of them use the GetOwner method  to get the owners names and domain. Create an object and ouput

May 31, 2017  4:42 PM

Get-PhysicalDisk options

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Disk storage, Powershell

These are the Get-PhysicalDisk options for identifying the disk you want

-UniqueId <string>

-ObjectId <string>

-FriendlyName <string>

-InputObject <CimInstance#MSFT_PhysicalDisk>

-StorageSubsystem <CimInstance#MSFT_StorageSubsystem>

-StorageEnclosure <CimInstance#MSFT_StorageEnclosure>

-StorageNode <CimInstance#MSFT_StorageNode>

-StoragePool <CimInstance#MSFT_StoragePool>

-VirtualDisk <CimInstance#MSFT_VirtualDisk>

When dealing with disks installed in the machine then the friendly names is the easiest to use

PS> Get-PhysicalDisk | Format-List UniqueId, ObjectId, FriendlyName
UniqueId     : 60022480233DF060FE631B8A4EDD93A0
ObjectId     : {1}\\W510W16\root/Microsoft/Windows/Storage/Providers_v2\SPACES_PhysicalDisk.ObjectId=”{1dab9cf6-a1b4-11e6-a890-806e6f6e6963}:PD:{12e941a8-6125-c008-8806-8868642331ef}”
FriendlyName : Msft Virtual Disk

UniqueId     : {d8e80f34-22bc-0a36-b302-d96abe30a6cc}
ObjectId     : {1}\\W510W16\root/Microsoft/Windows/Storage/Providers_v2\SPACES_PhysicalDisk.ObjectId=”{1dab9cf6-a1b4-11e6-a890-806e6f6e6963}:PD:{d8e80f34-22bc-0a36-b302-d96abe30a6cc}”
FriendlyName : Samsung SSD 840 PRO Series


May 30, 2017  1:55 PM

String casing

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

There are times when you may want to change string casing. You have a couple of options.

There are a couple of methods on the string class that you can use to modify the case of a string.

PS> 'aaa'.ToUpper()
AAA

PS> 'AAA'.ToLower()
aaa

Alternatively you can use the culture information

PS> (Get-Culture).TextInfo.ToLower('AAA')
aaa
 PS> (Get-Culture).TextInfo.ToUpper('aaa')
 AAA
 PS> (Get-Culture).TextInfo.ToTitleCase('aaa')
Aaa

The interesting one is To Titlecase which will capitalise the first letter and make the rest lower case

PS> (Get-Culture).TextInfo.ToTitleCase('aaBaaC')
Aabaac

At least it does for my culture settings

PS> Get-Culture

LCID             Name             DisplayName
 ----             ----             -----------
 2057             en-GB            English (United Kingdom)

You’ll need to test what it does if you have a different culture setting


May 29, 2017  12:51 PM

Build a better pull server

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Desired State Configuration, Powershell

DSC functions in 2 modes – push (most basic) and pull. Creating a pull server is a non-trivial task and the out-of-the-box pull server has some issues. Some of the folks at powershell.org have decided its time to build a better pull server.

There’s a project on github that supplies the code for the open source, cross platform, pull server project known as tug.  pull – tug – pull… you get the picture.

You can find the project at https://github.com/PowerShellOrg/tug

If you’re using DSC download it, give it a try and feed back to the project what you’ve discovered.


May 26, 2017  8:53 AM

Diskpart and PowerShell – part 6: Multiple partitions on a disk

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Disk storage, Powershell

So far we’ve looked at creating a single partition on a disk. This time we’ll look at how you can create multiple partitions on a disk. The are good reasons not to do this but its something I’ve seen done on a frequent basis.

Lets create a 20GB disk as an example and mount it

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 20GB
Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

Initialise the disk

Initialize-Disk -Number 1

Now we can create some partitions

New-Partition -DiskNumber 1 -DriveLetter F -Size 5GB
New-Partition -DiskNumber 1 -DriveLetter G -Size 5GB
New-Partition -DiskNumber 1 -DriveLetter H -Size 5GB
New-Partition -DiskNumber 1 -DriveLetter I -Size 4.87GB

The reason that the last partition is only 4.87 G is that 128MB of disk space is reserved

PS> Get-Partition -DiskNumber 1 | Format-Table -AutoSize


   DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#2&1f4adffe&0&000003#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

PartitionNumber DriveLetter Offset         Size Type
--------------- ----------- ------         ---- ----
1                           17408        128 MB Reserved
2               F           135266304      5 GB Basic
3               G           5503975424     5 GB Basic
4               H           10872684544    5 GB Basic
5               I           16241393664 4.87 GB Basic

You can format the 4 new volumes in one pass

Get-Partition -DiskNumber 1 | 
where Type -ne 'Reserved' | 
Format-Volume -FileSystem NTFS -Confirm:$false –Force

PS> Get-Partition -DiskNumber 1 | Get-Volume | select DriveLetter, FileSystem, Size

DriveLetter FileSystem       Size
----------- ----------       ----
          H NTFS       5368705024
          G NTFS       5368705024
          I NTFS       5229244416
          F NTFS       5368705024

The Storage module can be used to simply and easily create multiple volumes on a disk


May 23, 2017  11:35 AM

PowerShell in Azure Cloud Shell

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Azure, Powershell

if you are an Azure user see this post from the PowerShell team about the Azure Cloud Shell – https://blogs.msdn.microsoft.com/powershell/2017/05/23/coming-soon-powershell-in-azure-cloud-shell/


May 22, 2017  1:11 PM

Diskpart and PowerShell – part4: Remove a partition

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Disk storage, Powershell

So far you’ve seen how to create and modify partitions and volumes. Its now time to look at how you remove a partition.

Mount the test VHD

Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

You can’t remove a volume – you have to remove the partition. Identifying the CORRECT partition to remove is the challenge

PS> Get-Partition | select PartitionNumber, DriveLetter, Size, Type

PartitionNumber DriveLetter         Size Type
--------------- -----------         ---- ----
              1                134217728 Reserved
              2           F   8589934592 Basic
              1                367001600 IFS
              2           C 511269232640 IFS
              3                470810624 Unknown

Not every partition has a drive letter and partition numbers are repeated. The partition object holds the disk number

PS> Get-Partition | select DiskNumber, PartitionNumber, DriveLetter, Size, Type | Format-Table

DiskNumber PartitionNumber DriveLetter         Size Type
---------- --------------- -----------         ---- ----
         1               1                134217728 Reserved
         1               2           F   8589934592 Basic
         0               1                367001600 IFS
         0               2           C 511269232640 IFS
         0               3                470810624 Unknown

So the combination of disk number and partition number is unique and will identify any partition. Remove our 8GB partition

Remove-Partition -DiskNumber 1 -PartitionNumber 2 -Confirm:$false

and the 128MB partition

Remove-Partition -DiskNumber 1 -PartitionNumber 1 -Confirm:$false

Get-Partition will show that the drive F: has been removed

Looking the disk organisation

Get-Disk -Number 1 | Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, @{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, @{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} | Format-List

Size              : 20
AllocatedSize     : 0
LargestFreeExtent : 20

The whole of the disk is now available for re-use


May 21, 2017  1:50 PM

Diskpart and PowerShell–part 4: Expand a volume

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Disk storage, Powershell

You’ve seen how to create a volume but how do you expand a volume?

Let’s create a new disk and mount it

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 20GB
Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD
Initialize-Disk -Number 1

This time we’ll create a volume that only uses part of the disk

New-Partition -DiskNumber 1 -DriveLetter F -Size 5GB

And now format the partition

Get-Partition -DriveLetter F |
Format-Volume -FileSystem NTFS -Confirm:$false –Force

The disk is organised like this

Get-Disk -Number 1 | 
Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, 
 @{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, 
@{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} |
Format-List

Size              : 20
AllocatedSize     : 5.13
LargestFreeExtent : 14.87

Lets expand the partition

Get-Partition -DriveLetter F | 
Resize-Partition -Size 8GB

And re-examine the disk organisation

Get-Disk -Number 1 | 
Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, 
 @{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, 
@{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} |
Format-List

Size              : 20
AllocatedSize     : 8.13
LargestFreeExtent : 11.87

The extra space is added to the volume and formatted to match the existing filesystem on the volume


May 18, 2017  2:24 PM

Table or List

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A question on the forum asked why a object is displayed in a table if it has 4 or fewer properties and as  a list if it  has more than 4 properties:

PS> [PSCustomObject]@{P1=1; P2=2; P3=3; P4=4}

P1 P2 P3 P4
-- -- -- --
 1  2  3  4


PS> [PSCustomObject]@{P1=1; P2=2; P3=3; P4=4; P5=5}


P1 : 1
P2 : 2
P3 : 3
P4 : 4
P5 : 5

This is a built in mechanism in PowerShell and is done automatically.

The quick way to get the output in table format is to use Format-Table

PS> [PSCustomObject]@{P1=1; P2=2; P3=3; P4=4; P5=5} | Format-Table

P1 P2 P3 P4 P5
-- -- -- -- --
 1  2  3  4  5


May 10, 2017  3:39 AM

PowerShell v6 beta

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell v6 has reached a significant milestone – the release of the first PowerShell v6 beta version. There have been 18 releases of alpha code since August 2016 when the open source PowerShell v6 project started.

There is no indication of how many beta releases there will be before PowerShell v6 is ready to ship.

Code is available from https://github.com/PowerShell/PowerShell/releases


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: