- <#
- .SYNOPSIS
- This script displays a message box and then processes it
- .DESCRIPTION
- This script firsts creates a wscript.shell object and
- invokes the popup method to display a message. The script
- then processes the response to the geroup (timeout, yes, no).
- .NOTES
- File Name : Show-MessageBox.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/x83z1d9f%28VS.84%29.aspx
- .EXAMPLE
- Left as an exercise to the reader!
- #>
- # Create the shell object
- $WshShell = New-Object -Com Wscript.Shell
- # Call the Popup method with a 7 second timeout.
- $Btn = $WshShell.Popup("Do you feel alright?", 7, "Question:", 0x4 + 0x20)
- # Process the response
- switch ($Btn) {
- # Yes button pressed.
- 6 {"Glad to hear you feel alright."}
- # No button pressed.
- 7 {"Hope you're feeling better soon."}
- # Timed out.
- -1 {"Is there anybody out there?"}
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Friday, 30 December 2011
Show-MessageBox.ps1
Monday, 12 December 2011
Get-PortAndProtocolFromUrl.ps1
- <#
- .SYNOPSIS
- This script strips out a port and protocol number from a URL
- .DESCRIPTION
- This script creates a regular expression reged then uses it to
- match against the URL to get the protocol and port. This is a
- re-write of the MSDN sample.
- .NOTES
- File Name : Get-PortAndProtocolFromUrl.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/63ew9az0.aspx
- .EXAMPLE
- PowerShell> .\Get-PortAndProtocolFromUrl.ps1
- Port : http
- Protocol: 8080
- #>
- # Set URL
- $url = "http://www.contoso.com:8080/letters/readme.html"
- # Create Regex, then match against the URL
- $r = new-object System.Text.RegularExpressions.Regex "^(?<proto>\w+)://[^/]+?:(?<port>\d+)?/"
- $m = $r.Match($url)
- # Print results
- if ($m.Success) {
- "Port : {0}" -f $M.groups["proto"].value
- "Protocol: {0}" -f $M.groups["port"].value
- }
Labels:
Regex,
System.Text.RegularExpressions
Wednesday, 7 December 2011
Confirm-ValidEmailAddress.ps1
- <#
- .SYNOPSIS
- This script validates email addresses based on
- MSFT published Regular Expression. This is a
- re-write with PowerShell of an existing bit of
- MSDN sample code
- .DESCRIPTION
- This script first creates a function to validate
- an email address. It uses a large regex that is
- documented at the MSDN page noted below. The script
- then creates an array of email addreses and then
- validates them against the function and displays
- the results.
- .NOTES
- File Name : Confirm-ValidEmailAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/12/confirm-validemailaddressps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/01escwtf.aspx
- .EXAMPLE
- Valid: david.jones@proseware.com
- Valid: d.j@server1.proseware.com
- Valid: jones@ms1.proseware.com
- Invalid: j.@server1.proseware.com
- Invalid: j@proseware.com9
- Valid: js#internal@proseware.com
- Valid: j_9@[129.126.118.1]
- Invalid: j..s@proseware.com
- Invalid: js*@proseware.com
- Invalid: js@proseware..com
- Invalid: js@proseware.com9
- Valid: j.s@server1.proseware.com
- Valid: tfl@psp.co.uk
- Valid: cuddly.penguin@cookham.net
- #>
- Function IsValidEmail {
- Param ([string] $In)
- # Returns true if In is in valid e-mail format.
- [system.Text.RegularExpressions.Regex]::IsMatch($In,
- "^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
- "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
- } # End of IsValidEmail
- [string[]] $emailAddresses = "david.jones@proseware.com", "d.j@server1.proseware.com",
- "jones@ms1.proseware.com", "j.@server1.proseware.com",
- "j@proseware.com9", "js#internal@proseware.com",
- "j_9@[129.126.118.1]", "j..s@proseware.com",
- "js*@proseware.com", "js@proseware..com",
- "js@proseware.com9", "j.s@server1.proseware.com",
- "tfl@psp.co.uk", "cuddly.penguin@cookham.net"
- ForEach ($emailAddress in $emailAddresses) {
- if (IsValidEmail($emailAddress)) {
- "Valid: {0}" -f $emailAddress
- }
- else {
- "Invalid: {0}" -f $emailAddress
- }
- }
Sunday, 20 November 2011
Show-FileInformation.ps1
- <#
- .SYNOPSIS
- This script displays information returned from the
- file version object.
- .DESCRIPTION
- This script gets, then displays, all the information returned
- from the System.Diagnostics.Fileinfo of Notepad.exe
- .NOTES
- File Name : Show-FileInformation.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://http://pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo_properties.aspx
- .EXAMPLE
- Psh> .\Show-FileInformation.ps1
- File Major Part for C:\Windows\system32\Notepad.exe is: 6
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Display all the file information:
- "FileInfo information for {0}" -f $file
- "Comments : {0}" -f $myFileVersionInfo.Comments
- "Company Name : {0}" -f $myFileVersionInfo.CompanyName
- "FileBuldPart : {0}" -f $myFileVersionInfo.FileBuildPart
- "FileDescription : {0}" -f $myFileVersionInfo.FileDescription
- "FileMajorPart : {0}" -f $myFileVersionInfo.FileMajorPart
- "FileMinorPart : {0}" -f $myFileVersionInfo.FileMinorPart
- "FilePrivatePart : {0}" -f $myFileVersionInfo.FilePrivatePart
- "FileName : {0}" -f $myFileVersionInfo.FileName
- "FileVersion : {0}" -f $myFileVersionInfo.FileVersion
- "InternalName : {0}" -f $myFileVersionInfo.InternalName
- "IsDebug : {0}" -f $myFileVersionInfo.IsDebug
- "IsPatched : {0}" -f $myFileVersionInfo.IsPatched
- "IsPreRelease : {0}" -f $myFileVersionInfo.IsPreRelease
- "IsPrivateBuild : {0}" -f $myFileVersionInfo.IsPrivateBuild
- "IsSpecialBuild : {0}" -f $myFileVersionInfo.IsSpecialBuild
- "Language : {0}" -f $myFileVersionInfo.Language
- "LegalCopyright : {0}" -f $myFileVersionInfo.LegalCopyright
- "LegalTrademarks : {0}" -f $myFileVersionInfo.LegalTrademarks
- "OriginalFilename : {0}" -f $myFileVersionInfo.OriginalFilename
- "PrivateBuild : {0}" -f $myFileVersionInfo.PrivateBuild
- "ProductBuildPart : {0}" -f $myFileVersionInfo.ProductBuildPart
- "ProductMajordPart : {0}" -f $myFileVersionInfo.ProductMajorPart
- "ProductMinorPart : {0}" -f $myFileVersionInfo.ProductMinorPart
- "ProductName : {0}" -f $myFileVersionInfo.ProductName
- "ProductPrivatePart : {0}" -f $myFileVersionInfo.ProductMinorPart
- "ProductVersion : {0}" -f $myFileVersionInfo.ProductVersion
- "SpecialBuild : {0}" -f $myFileVersionInfo.SpecialBuild
Sunday, 13 November 2011
ShowFileDescription.ps1
- <#
- .SYNOPSIS
- This script shows the build description of file version object.
- .DESCRIPTION
- This script is a re-implementation of an MSDN Sample script
- that uses System.Diagnostics.FileVersionInfo to get
- the description of the file.
- .NOTES
- File Name : Show-FileDescription.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/11/showfiledescriptionps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filedescription.aspx
- .EXAMPLE
- Psh> .\Show-FileDescription.ps1
- File description for C:\Windows\system32\Notepad.exe is: Notepad
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Print the Build details name
- "File description for {0} is: {1}" -f $file,$myFileVersionInfo.FileDescription
Show-FileBuildPart.ps1
- <#
- .SYNOPSIS
- This script shows the build number of file version object.
- .DESCRIPTION
- This script is a re-implementation of an MSDN Sample script
- that uses System.Diagnostics.FileVersionInfo to get
- the build identification of the file.
- .NOTES
- File Name : Show-FileBuildPart.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/11/show-filebuildpartps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filebuildpart.aspx
- .EXAMPLE
- Psh> .\Show-FileBuildPart.ps1
- File build number for C:\Windows\system32\Notepad.exe is: 6001
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Print the Build details name
- "File build number for {0} is: {1}" -f $file,$myFileVersionInfo.FileBuildPart
Saturday, 12 November 2011
Show-NumberPadding3.ps1
- <#
- .SYNOPSIS
- Shows formatting of double values.
- .DESCRIPTION
- This script, a re-implementation of an MSDN Sample,
- creates a double value then formats it with 5 leading
- zeros.
- .NOTES
- File Name : Show-NumberPadding3.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/dd260048.aspx
- .EXAMPLE
- Psh> Show-NumberPadding3.ps1
- 01053240
- 00103932.52
- 01549230
- 01053240
- 00103932.52
- 01549230
- 9034521202.93
- #>
- $fmt = "00000000.##"
- [int] $intValue = 1053240
- [decimal] $decValue = 103932.52
- [float] $sngValue = 1549230.10873992
- [double] $dblValue = 9034521202.93217412
- # Display the numbers using the ToString method
- $intValue.ToString($fmt)
- $decValue.ToString($fmt)
- $sngValue.ToString($fmt)
- ""
- # Display the numbers using composite formatting
- $formatString = " {0,15:" + $fmt + "}" # right justified
- $formatString -f $intValue
- $formatString -f $decValue
- $formatString -f $sngValue
- $formatString -f $dblValue
Labels:
formatting,
powershell,
PowerShell scripts,
ToString()
Show-NumberPadding1.ps1
- <#
- .SYNOPSIS
- Shows formatting of leading Zeros
- .DESCRIPTION
- This script, a re-implementation of an MSDN Sample,
- creates several numbers of varying type, then
- displays them using .NET Formatting. The second set
- of formatting shows the difference between .ToString()
- and composite format strings to format - to approaches
- that accomplish the same goal!
- .NOTES
- File Name : Show-NumberPadding1.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/11/show-numberpadding1ps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/dd260048.aspx
- .EXAMPLE
- Psh> Show-NumberPadding1.ps1
- 00000254 000000FE
- 00010342 00002866
- 01023983 000F9FEF
- 06985321 006A9669
- 9223372036854775807 7FFFFFFFFFFFFFFF
- 18446744073709551615 FFFFFFFFFFFFFFFF
- 00000254 000000FE
- 00010342 00002866
- 01023983 000F9FEF
- 9223372036854775807 7FFFFFFFFFFFFFFF
- 18446744073709551615 FFFFFFFFFFFFFFFF
- #>
- [byte] $byteValue = 254
- [int16] $shortValue = 10342
- [int] $intValue = 1023983
- [long] $lngValue = 6985321
- [long] $lngValue2 = [System.Int64]::MaxValue
- [UInt64] $ulngValue = [System.UInt64]::MaxValue
- # Display integer values by caling the ToString method.
- "{0,22} {1,22}" -f $byteValue.ToString("D8") , $byteValue.ToString("X8")
- "{0,22} {1,22}" -f $shortValue.ToString("D8"), $shortValue.ToString("X8")
- "{0,22} {1,22}" -f $intValue.ToString("D8") , $intValue.ToString("X8")
- "{0,22} {1,22}" -f $lngValue.ToString("D8") , $lngValue.ToString("X8")
- "{0,22} {1,22}" -f $lngValue2.ToString("D8") , $lngValue2.ToString("X8")
- "{0,22} {1,22}" -f $ulngValue.ToString("D8") , $ulngValue.ToString("X8")
- ""
- # Display the same integer values by using composite formatting
- "{0,22:D8} {0,22:X8}" -f $byteValue
- "{0,22:D8} {0,22:X8}" -f $shortValue
- "{0,22:D8} {0,22:X8}" -f $intValue
- "{0,22:D8} {0,22:X8}" -f $lngValue2
- "{0,22:D8} {0,22:X8}" -f $ulngValue
Labels:
formatting,
powershell,
PowerShell scripts
Thursday, 10 November 2011
Show-FileVersionCompanyName
- <#
- .SYNOPSIS
- This script shows the company name of a file version object
- .DESCRIPTION
- This script is a re-implementation of an MSDN Sample script
- that uses System.Diagnostics.FileVersionInfo to get
- the company name (if it exists).
- .NOTES
- File Name : Show-FileVersionCompanyName.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.companyname.aspx
- .EXAMPLE
- Psh> .\Show-FileVersionCompanyName.ps1
- Company name: Microsoft Corporation
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Print the Company name
- "Company name: " + $myFileVersionInfo.CompanyName
Show-FileVersionComments.ps1
- <#
- .SYNOPSIS
- This script shows the comments of a file version object
- .DESCRIPTION
- This script is a re-implementation of an MSDN Sample script
- that uses System.Diagnostics.FileVersionInfo to get
- the company name (if it exists).
- .NOTES
- File Name : Show-FileVersionComments.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.comments.aspx
- .EXAMPLE
- Psh> .\Show-FileVersionComments.ps1
- Comments : Microsoft Corporation
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Print the Comments field
- "Commments : " + $myFileVersionInfo.CompanyName
Technorati Tags: PowerShell,Scripts,PowerShell script,System.Diagnostics.FileVersionInfo,GetVersionInfo
Wednesday, 9 November 2011
Open-WordDocument.ps1
- <#
- .SYNOPSIS
- This script opens a word document using PowerShell
- .DESCRIPTION
- This script re-implments a simple MSDN script to open
- a word document using VBA. IT Pros using PowerShell
- might also make use of to create rich documents as
- output from a script.
- .NOTES
- File Name : Open-WordDocument.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/bb148369%28office.12%29.aspx
- .EXAMPLE
- Running this sample opens the file c:\foo\doc1.docx in Word. The
- COM object referring to the document is returned for later use.
- #>
- # Create Word Object
- $wrd = new-object -com word.application
- # Make Word Visible
- $wrd.visible = $true
- # Open a document
- $doc = $wrd.documents.open("C:\foo\doc1.docx")
Labels:
COM,
powershell,
PowerShell scripts,
Word.Application
Tuesday, 8 November 2011
Show-TimeSpan.ps1
- <#
- .SYNOPSIS
- This script, a re-implementation of an MSDN sample, shows
- details of a timespan object
- .DESCRIPTION
- This script re-implements a simple MSDN script that creates a
- timespan object and displays its properties.
- .NOTES
- File Name : Show-TimeSpan.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.timespan.aspx
- .EXAMPLE
- Psh> .\Show-TimeSpan.ps1
- 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
- Value of Days Component: 229
- Total Number of Days: 229.229340277778
- Value of Hours Component: 5
- Total Number of Hours: 5501.50416666667
- Value of Minutes Component: 30
- Total Number of Minutes: 330090.25
- Value of Seconds Component: 15
- Total Number of Seconds: 19,805,415
- Value of Milliseconds Component: 0
- Total Number of Milliseconds: 19,805,415,000
- Ticks: 198,054,150,000,000
- #>
- # Define two dates
- $date1 = new-object system.datetime 2010, 1, 1, 8, 0, 15
- $date2 = new-object system.datetime 2010, 8, 18, 13, 30, 30
- # Create a time Interval
- $interval = New-Timespan -start $date1 -end $date2
- #Display the interval
- "{0} - {1} = {2}" -f $date2, $date1, $interval.ToString()
- # Display individual properties of the resulting TimeSpan object.
- " {0,-35} {1,20}" -f "Value of Days Component:", $interval.Days
- " {0,-35} {1,20}" -f "Total Number of Days:", $interval.TotalDays
- " {0,-35} {1,20}" -f "Value of Hours Component:", $interval.Hours
- " {0,-35} {1,20}" -f "Total Number of Hours:", $interval.TotalHours
- " {0,-35} {1,20}" -f "Value of Minutes Component:", $interval.Minutes
- " {0,-35} {1,20}" -f "Total Number of Minutes:", $interval.TotalMinutes
- " {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $interval.Seconds
- " {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $interval.TotalSeconds
- " {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $interval.Milliseconds
- " {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $interval.TotalMilliseconds
- " {0,-35} {1,20:N0}" -f "Ticks:", $interval.Ticks
Labels:
Power,
PowerShell scripts,
system.datetime,
System.TImespan
Show-UnicodeCharacters.ps1
- <#
- .SYNOPSIS
- This script, a re-implementation of an MSDN sample, shows the
- Unicode details of a unicode character, using PowerShell.
- .DESCRIPTION
- This script re-implements a simple MSDN script that takes a Unicode Character
- and uses CharUnicodeInfo class to get details of that character, which are then
- displayed.
- .NOTES
- File Name : Show-UnicodeCharacters.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/11/show-unicodecharactersps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.charunicodeinfo.aspx
- .EXAMPLE
- Psh > .\show-unicodecharacters.ps1
- c Num Dig Dec UnicodeCategory
- U+0061 LATIN SMALL LETTER A a -1 -1 -1 LowercaseLetter
- U+0393 GREEK CAPITAL LETTER GAMMA Γ -1 -1 -1 UppercaseLetter
- U+0039 DIGIT NINE 9 9 9 9 DecimalDigitNumber
- U+00B2 SUPERSCRIPT TWO ² 2 2 -1 OtherNumber
- U+00BC VULGAR FRACTION ONE QUARTER ¼ 0.25 -1 -1 OtherNumber
- U+0BEF TAMIL DIGIT NINE ௯ 9 9 9 DecimalDigitNumber
- U+0BF0 TAMIL NUMBER TEN ௰ 10 -1 -1 OtherNumber
- U+0F33 TIBETAN DIGIT HALF ZERO ༳ -0.5 -1 -1 OtherNumber
- U+2788 CIRCLED SANS-SERIF DIGIT NINE ➈ 9 9 -1 OtherNumber
- #>
- # Helper Function
- Function PrintProperties {
- param ($char)
- $fmtstring = " {0,-5} {1,-8} {2,-9} {3,-9} {4,-9}"
- $a = $char
- $b = [System.Globalization.CharUnicodeInfo]::GetNumericValue( $char )
- $c = [System.Globalization.CharUnicodeInfo]::GetDigitValue( $char )
- $d = [System.Globalization.CharUnicodeInfo]::GetDecimalDigitValue( $char )
- $e = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory( $char )
- $fmtstring -f $a, $b, $c, $d, $e
- }
- " c Num Dig Dec UnicodeCategory"
- "U+0061 LATIN SMALL LETTER A " + (PrintProperties "a")
- "U+0393 GREEK CAPITAL LETTER GAMMA " + (PrintProperties ([Char] 0x0393) )
- "U+0039 DIGIT NINE " + (PrintProperties "9")
- "U+00B2 SUPERSCRIPT TWO " + (PrintProperties $([Char] 0x00B2) )
- "U+00BC VULGAR FRACTION ONE QUARTER " + (PrintProperties $([Char] 0x00BC) )
- "U+0BEF TAMIL DIGIT NINE " + (PrintProperties $([Char] 0x0BEF) )
- "U+0BF0 TAMIL NUMBER TEN " + (PrintProperties $([Char] 0x0BF0) )
- "U+0F33 TIBETAN DIGIT HALF ZERO " + (PrintProperties $([Char] 0x0F33) )
- "U+2788 CIRCLED SANS-SERIF DIGIT NINE " + (PrintProperties $([Char] 0x2788) )
Tuesday, 1 November 2011
Show-DateTimeFormatInfo.ps1
- <#
- .SYNOPSIS
- This script re-implements and MSDN sample that shows
- the different Date/Time Formatting characters and
- how they are used in formatting date/time objects.
- .DESCRIPTION
- This script Creates a date/time object, then shows formatting
- using the key Date/Time Format strings.
- .NOTES
- File Name : Show-DateTimeFormatInfo.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
- .EXAMPLE
- Left as an exercise for the reader.
- #>
- # Create and initialise a DateTimeFormatInfo associated with the en-US culture.
- $MyDTFI = (new-object System.Globalization.CultureInfo "en-US", $false ).DateTimeFormat
- # Create a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3)
- # The Gregorian calendar is the default calendar for the en-US culture
- $MyDT = new-object System.DateTime 2002, 1, 3
- # Display the format pattern associated with each format character
- "FORMAT en-US EXAMPLE"
- "CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY"
- " d {0}" -f $MyDT.ToString("d", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.ShortDatePattern, "(ShortDatePattern)"
- " D {0}" -f $MyDT.ToString("D", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.LongDatePattern, "(LongDatePattern)"
- " f {0}`n" -f $MyDT.ToString("f", $MyDTFI)
- " F {0}" -f $MyDT.ToString("F", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.FullDateTimePattern, "(FullDateTimePattern)"
- " g {0}`n" -f $MyDT.ToString("g", $MyDTFI)
- " G {0}`n" -f $MyDT.ToString("G", $MyDTFI)
- " m {0}" -f $MyDT.ToString("m", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.MonthDayPattern, "(MonthDayPattern)"
- " M {0}" -f $MyDT.ToString("M", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.MonthDayPattern, "(MonthDayPattern)"
- " o {0}`n" -f $MyDT.ToString("o", $MyDTFI)
- " r {0}" -f $MyDT.ToString("r", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.RFC1123Pattern, "(RFC1123Pattern)"
- " R {0}" -f $MyDT.ToString("R", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.RFC1123Pattern, "(RFC1123Pattern)"
- " s {0}" -f $MyDT.ToString("s", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.SortableDateTimePattern, "(SortableDateTimePattern)"
- " t {0}" -f $MyDT.ToString("t", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.ShortTimePattern, "(ShortTimePattern)"
- " T {0}" -f $MyDT.ToString("T", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.LongTimePattern, "(LongTimePattern)"
- " u {0}" -f $MyDT.ToString("u", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)"
- " U {0}`n" -f $MyDT.ToString("U", $MyDTFI)
- " y {0}" -f $MyDT.ToString("y", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.YearMonthPattern, "(YearMonthPattern)"
- " Y {0}" -f $MyDT.ToString("Y", $MyDTFI)
- " {0} {1}`n" -f $MyDTFI.YearMonthPattern, "(YearMonthPattern)"
Subscribe to:
Posts (Atom)