How to Change File Properties (ReadOnly, CreationTime etc.) Using PowerShell

Changing file properties is quite easy with PowerShell.

 

Remove ReadOnly attribute:

Set-ItemProperty -Path 'C:\Program Files\desktop.ini' -Name IsReadOnly -Value $false

 

Change CreationTime (90 days back in time):

Set-ItemProperty -Path 'C:\Program Files\desktop.ini' -Name CreationTime -Value ((Get-Date).AddDays(-90))

 

 

 

Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell

Windows PowerShell introduced parameter validation which drastically reduces lines of validation code – and replaces it with a couple of markup tags:

 

ValidateCount (int minLength, int maxlength)
How many arguments are allowed for a parameter.

[ValidateCount(1,3)]
[String[]]
$Collection

ValidateLength (int minLength, int maxlength)

Length (in characters) of a parameter argument.

[ValidateLength(1,8)]
[String[]]
$Phone

ValidatePattern (string regexString, Named Parameters)
Valid patterns for a parameter argument.

[ValidatePattern("[0-9][0-9][0-9][0-9]")]
[String[]]
$Pin

ValidateRange (object minRange, object maxRange)
Valid range for a parameter argument.

[ValidateRange(18,120)]
[Int]
$Number

ValidateSet (string[] validValues, Named Parameters)
Possible values for a parameter argument.

[ValidateSet("One","Two","Three","Four")]
[String]
$TextString

ValidateScript (scriptblock validValues)
Custom script for validation

[ValidateScript({Test-Path $_ -PathType 'Container'})]
[String]
$Path

ValidateNotNull ()
Input is not null

[ValidateNotNull()]
[String]
$Id

ValidateNotNullOrEmpty ()
Input is neither null or empty

[ValidateNotNullOrEmpty()]
[String[]]
$UserName

 

Read more about validating parameters on MSDN:

Or use PowerShell built-in help:

 

Write Event to Windows Event Log using PowerShell

 

Creating an EventLog entry using PowerShell is shown below:

Write-EventLog -LogName 'Application' -Source 'MyEventSource' -EventID 1000 -EntryType Warning -Message "This is a test of writing to the Event Log using PowerShell."

 

Note: The Event Source needs to be created before writing entries to the Event Log. (See this post for further info: Create Event Source with PowerShell)

 

 

Create Event Source with PowerShell

To create a Event Source in PowerShell use this:

$Source = "MyEventSource"
if ([System.Diagnostics.EventLog]::SourceExists($Source) -eq $false {
  [System.Diagnostics.EventLog]::CreateEventSource($Source, "Application")
}

Get more info on CreateEventSource method here : http://msdn.microsoft.com/en-us/library/2awhba7a.aspx

 

In PowerShell 2.0 this is even simpler:

$Source = "MyEventSource"
New-EventLog -LogName Application -Source $Source

 

Handling attributes with hyphens in PowerShell

Sometimes we need working with PowerShell objects which have attributes with hyphens. That can be a bit difficult as PowerShell assumes hyphens are used with operators and parameters.

Below is an example on how to get around this, and rename the attribute:

 

 

$Users = Get-AdUser -Filter * -Property "msDS-ResultantPSO" | Select name, @{Name="ResultantPSO";Expression={$_."msDS-ResultantPSO"}}

 

 

Enumerate COM classes using WMI

 

Quick and short way of enumerating COM classes is using the WMI class Win32_ClassicComClassSetting.

Example:

 

Get-WmiObject win32_ClassicComClassSetting | Select-Object ProgID | Group-Object ProgID | Select-Object Name | Sort-Object Name

 

Fun with Microsoft Agent and PowerShell

Show Merlin:

$agent = new-object -com Agent.Control.2
$agent.Connected = 1
$agent.Characters.Load("Merlin")
$merlin = $agent.Characters.Character("Merlin")
$merlin.Show()

List animations:

$merlin.AnimationNames

 

Activate animation:

$merlin.Play("Congratulate")

 

Have Merlin tell when process was started:

Get-Process | select -First 10 | foreach {$merlin.Speak($_.ProcessName + " was started " + $_.StartTime) | out-null; Start-Sleep 5 }

 

Microsoft Agent is being deprecated and will not be included in future versions of the Windows operating system.