PowerShell for Windows Admins


March 5, 2014  2:34 PM

PowerShell Summit 2014 edging closer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Now we’re in March the Summit is getting closer.  Looking forward to see friends old & new and listening to some excellent sessions.

If you haven’t registered there’s still places available.

As a bonus one of the things you’ll take away is a module for working with the registry – filling one of gaps in the PowerShell cmdlets for Windows administrators

March 2, 2014  3:10 PM

PowerShell Jobs Series

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Today sees the start of my series of articles on PowerShell jobs for the Scripting Guy blog.

First article is here -

http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/02/powershell-jobs-week-introduction-to-powershell-jobs.aspx

 

with the associated Power Tip here

http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/02/powertip-control-job-starts-with-powershell.aspx


February 28, 2014  4:44 PM

do and while

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Do you understand the difference between do loops and while loops?  On the surface they appear very similar.

£> do{$i++}while($i -lt 10) £> $i 10

£> $i=0 £> while($i -lt 10){$i++} £> $i 10

BUT there is a difference

£> $i=11 £> do{$i++}while($i -lt 10) £> $i 12

£> $i=11 £> while($i -lt 10){$i++} £> $i

Notice the difference in the final result

A DO loop will ALWAYS execute at least once because the test is at the end.

A while loop has the test at the beginning so may not execute at all.

I’ve seen this cause people real difficulty at times so think about where you need the test to be, if the loop needs to execute at least once and what the final value of any incrementing variables should be


February 28, 2014  2:33 PM

Automatically create folder for home drive

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In this post  http://richardspowershellblog.wordpress.com/2013/10/28/setting-ad-attributes-from-a-csv-file/

I showed how to modify the user’s home folder setting in Active Directory.

A comment was recently left asking about automatically creating the folder on the fileserver and creating the share that is associated with it.

This isn’t a simple exercise – you will need a script to:

You can create the folder using New-Item

New-Item -Path c:\test -Name anyolduser -Type Directory

You can share it

$max = [uint32]5

$type = [uint32]0

Invoke-CimMethod -ClassName Win32_Share -MethodName Create -Arguments @{Name=’anyolduser’; Path=’c:\test\anyolduser’; Type=$type; MaximumAllowed=$max; Description=’anyolduser – homedrive’}

And then you have to set share and NTFS permissions according to your organization’s policies


February 28, 2014  11:55 AM

File or Folder?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This is a common type of use for Get-ChildItem:

Get-ChildItem -Path c:\temp

However, you could get files or folders returned.  Very often you just want to see the files

In PowerShell 1.0 & 2.0 you had to do this:

Get-ChildItem -Path c:\temp   | where {-not $_.PSIsContainer}

If you want to see the subfolders only then you reverse the condition:

Get-ChildItem -Path c:\temp  | where {$_.PSIsContainer}

In PowerShell 3.0 Get-ChildItem got some new parameters –File and –Directory. They display files only or folders only respectively. The default action is still to display files and folders.

Get-ChildItem -Path c:\temp  -File  Get-ChildItem -Path c:\temp  -Directory

I still see a lot of people making more work for themselves because they don’t use or don’t know about the new parameters.


February 27, 2014  4:20 PM

I’m afraid you can’t do that anymore

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In PowerShell 1.0 you could do this:

notepad

$proc = Get-WmiObject -Class Win32_Process -Filter “Name=’notepad.exe’”

$proc.Terminate()

To access the methods of the WMI class you had to get a variable representing the instance and call the method. This technique still works in PowerShell 4.0

When the CIM cmdlets were introduced in PowerShell 3.0 people assumed that they worked the same:

£> notepad

£> $proc = Get-CimInstance -ClassName Win32_Process -Filter “Name=’notepad.exe’”

£> $proc.Terminate()

Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named

‘Terminate’.

At line:1 char:1

+ $proc.Terminate()

+ ~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (Terminate:String) [], RuntimeException

+ FullyQualifiedErrorId : MethodNotFound

 

The CIM cmdlets retrun inert objects – NO METHODS – so you can’t do that.

The correct way to use the CIM cmdlets is:

Get-CimInstance -ClassName Win32_Process -Filter “Name=’notepad.exe’” | Invoke-CimMethod -MethodName Terminate

 


February 27, 2014  2:21 PM

Learn AD Management in a Month of Lunches–ebook available

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The ebook – PDF format – for Learn AD Management in a Month of Lunches has been published – http://www.manning.com/siddaway3/

If you bought the ebook as part of your MEAP you should be able to down load it – you’ll get or have got an email with the link. The printed version is at the printers and will be available on 12 March.

If you want the ebook in Kindle or epub versions they will be available 20 March.

Enjoy.


February 27, 2014  12:30 PM

PowerShell Summit NA 2014

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The PowerShell Summit is happening in Bellevue (Seattle) – April 28 – 30th.  You will be able to hear, meet and talk to some of the biggest names in PowerShell:

- Jeffrey Snover – the inventor of PowerShell

- PowerShell Team members

- Don Jones

- Jason Helmick

- Jeff Hicks

- Ed Wilson (The Scripting Guy)

- Steven Murawski

- Tome Tanasovski

- James O’Neill

I’ll be there delivering 3 sessions (WSMan cmdlets, cmdletizing the registry and using the network related cmdlets) – its the only chance this year of getting all three authors of PowerShell in Depth in the same place at the same time.

If you haven’t booked a place yet – you can register and view the rest of the sessions at http://powershell.org


February 26, 2014  4:47 PM

Useful storage cmdlets

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Scanning through the Storage module there is a bunch of useful cmdlets – starting with the Get* cmdlets:

Get-Command -Module Storage get*

Get-Disk

Get-DiskImage

Get-FileIntegrity

Get-FileStorageTier

Get-InitiatorId

Get-InitiatorPort

Get-MaskingSet

Get-OffloadDataTransferSetting

Get-Partition

Get-PartitionSupportedSize

Get-PhysicalDisk

Get-ResiliencySetting

Get-StorageJob

Get-StorageNode

Get-StoragePool

Get-StorageProvider

Get-StorageReliabilityCounter

Get-StorageSetting

Get-StorageSubSystem

Get-StorageTier

Get-StorageTierSupportedSize

Get-SupportedClusterSizes

Get-SupportedFileSystems

Get-TargetPort

Get-TargetPortal

Get-VirtualDisk

Get-VirtualDiskSupportedSize

Get-Volume

Get-VolumeCorruptionCount

Get-VolumeScrubPolicy

 

£> Get-PhysicalDisk | Format-List FriendlyName, CanPool, OperationalStatus, HealthStatus, Usage, Size

 

 

FriendlyName : PhysicalDisk0

CanPool : False

OperationalStatus : OK

HealthStatus : Healthy

Usage : Auto-Select

Size : 256060514304

 

 

 

£> Get-Disk | ft -a

 

Number Friendly Name OperationalStatus Total Size Partition Style

—— ————- —————– ———- —————

0 HFS256G3AMNB-2200A Online 238.47 GB GPT

 

£> Get-Partition | ft -a

 

 

Disk Number: 0

 

PartitionNumber DriveLetter Offset Size Type

————— ———– —— —- —-

1 1048576 350 MB Recovery

2 368050176 200 MB System

3 577765376 128 MB Reserved

4 C 711983104 231.85 GB Basic

5 249663848448 5.96 GB Recovery

 

£> Get-Volume | ft -a

 

DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size

———– ————— ———- ——— ———— ————- —-

C Windows NTFS Fixed Healthy 160.2 GB 231.85 GB

Windows RE tools NTFS Fixed Healthy 56.86 MB 350 MB

Recovery image NTFS Fixed Healthy 445.72 MB 5.96 GB

 


February 26, 2014  1:38 PM

Delete all but the last N

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I was asked a question about deleting files from a folder based on age.  The requirement was to delete all but the youngest N files.

One solution is a classic PowerShell one-liner. It is actually one PowerShell pipeline though I’ve split it across multiple lines for ease of reading.

Get-ChildItem -Path c:\temp -Filter *.tmp -File | sort LastWriteTime -Descending | select -Skip 5 | Remove-Item

Use Get-Childitem to read the files you’re interested in. I’ve used the –File parameetr to restrict the examination to files.

Sort the files on LastWriteTime – you could use CreationTime if the files haven’t been changed since creation. You want the results sorted in descending order so the latest files are at the top of the list. Younger dates are greater than older dates.

use Select-Object to skip the first N (in this case 5) files and pipe everything else into remove-item.  Add a –whatif parameter to Remove-Item if want to see how its working before letting it loose.

A nice simple answer with a use for Select-Object’s –Skip parameter that I hadn’t thought of before.


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: