Table of Contents
- Introduction
- Background
- VB Script to PowerShell
- Message to All Silver Members and Above
- Conclusion
- References
- Point of interest
- History
Introduction
The VBA developer as well as VB Script writer are very much familiar with the use of mostly common built-in library or function, for example CInt()
, CStr()
, Abs()
… etc. In this section, we will not discuss how to use this library or function in VB Script. We will discuss how we can convert and use library or function in <leo_highlight id="leoHighlights_Underline_0" style="DISPLAY: inline; BACKGROUND-ATTACHMENT: scroll; BACKGROUND-IMAGE: none; BORDER-BOTTOM: rgb(255,255,150) 2px solid; BACKGROUND-REPEAT: repeat; BACKGROUND-COLOR: transparent; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" önclick="leoHighlightsHandleClick('leoHighlights_Underline_0')" önmouseover="leoHighlightsHandleMouseOver('leoHighlights_Underline_0')" önmouseout="leoHighlightsHandleMouseOut('leoHighlights_Underline_0')" leohighlights_keywords="microsoft" leohighlights_removed_removed="http%3A//shortcuts.thebrowserhighlighter.com/leonardo/plugin/highlights/3_2/tbh_highlightsBottom.jsp?keywords%3Dmicrosoft%26domain%3Dwww.codeproject.com" leohighlights_underline="true">Microsoft Windows PowerShell.
Background
So, our main objective of this article is to put all mostly used libraries or functions & their use in <leo_highlight id="leoHighlights_Underline_1" style="DISPLAY: inline; BACKGROUND-ATTACHMENT: scroll; BACKGROUND-IMAGE: none; BORDER-BOTTOM: rgb(255,255,150) 2px solid; BACKGROUND-REPEAT: repeat; BACKGROUND-COLOR: transparent; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" önclick="leoHighlightsHandleClick('leoHighlights_Underline_1')" önmouseover="leoHighlightsHandleMouseOver('leoHighlights_Underline_1')" önmouseout="leoHighlightsHandleMouseOut('leoHighlights_Underline_1')" leohighlights_keywords="microsoft" leohighlights_removed_removed="http%3A//shortcuts.thebrowserhighlighter.com/leonardo/plugin/highlights/3_2/tbh_highlightsBottom.jsp?keywords%3Dmicrosoft%26domain%3Dwww.codeproject.com" leohighlights_underline="true">Microsoft Windows PowerShell under a single article.
It is very important to know that <leo_highlight id="leoHighlights_Underline_2" style="DISPLAY: inline; BACKGROUND-ATTACHMENT: scroll; BACKGROUND-IMAGE: none; BORDER-BOTTOM: rgb(255,255,150) 2px solid; BACKGROUND-REPEAT: repeat; BACKGROUND-COLOR: transparent; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" önclick="leoHighlightsHandleClick('leoHighlights_Underline_2')" önmouseover="leoHighlightsHandleMouseOver('leoHighlights_Underline_2')" önmouseout="leoHighlightsHandleMouseOut('leoHighlights_Underline_2')" leohighlights_keywords="microsoft" leohighlights_removed_removed="http%3A//shortcuts.thebrowserhighlighter.com/leonardo/plugin/highlights/3_2/tbh_highlightsBottom.jsp?keywords%3Dmicrosoft%26domain%3Dwww.codeproject.com" leohighlights_underline="true">Microsoft Windows PowerShell doesn’t have built-in methods, but we can achieve this by using the .NET Framework library.
Note
This article is a common place for all to participate with proper code snippets and full explanation.
VB Script to PowerShell
In this section, we will discuss how we can convert and use all the libraries or functions which are widely used in VB Script.
Functions / Methods
A list of functions / methods is given below:
Function / Methods | Function / Methods | Function / Methods | Function / Methods |
Abs() | Cos() | Split() | LBound() |
Asc() | CSng() | Eval() | LCase() |
CBool() | CStr() | Filter() | Left() |
CByte() | Date() | Hex() | Len() |
CCur() | DateAdd() | Hour() | LTrim() |
CDate() | DateDiff() | IsDate() | RTrim() |
CDbl() | DatePart() | IsEmpty() | Mid() |
Chr() | DateSerial() | IsNull() | Minute() |
CInt() | DateValue() | IsNumeric() | Month() |
CLng() | Day() | IsObject() | MonthName() |
Now() | Replace() | Right() | Round() |
Abs()
: Returns the absolute value of a number.
Example:
$result = [math]::abs(-99)
Output: 99
Asc()
: Returns the ANSI character code corresponding to the first letter in a string
.
Example:
$result = [byte][char] "A"
Output: 65
CBool()
: Returns an expression that has been converted to a Variant of subtype Boolean.
Example:
$result = 0 // 0 is for false & 1 is for true
$result = [bool] $result
Output: False
CByte()
: Returns an expression that has been converted to a Variant of subtype Byte
.
Example:
$result = "99.45"
$result = [byte] $result
Output: 99
CCur()
: Returns an expression that has been converted to a Variant of subtype Currency
.
Example:
$result = "{0:C}" -f 100
Output: $100.00
CDate()
: Returns an expression that has been converted to a Variant of subtype Date
.
Example:
$result =
$result = [datetime]$result
CDbl()
: Returns an expression that has been converted to a Variant of subtype Double
.
Example:
$result = "10.99"
$result = [double]$result
Output: 10.99
Chr()
: Returns the character associated with the specified ANSI character code.
Example:
$result = [char]42
Output: *
CInt()
: Returns an expression that has been converted to a Variant of subtype Integer
.
Example:
$result = "99.96"
$result = [int] $result
Output: 100
CLng()
: Returns an expression that has been converted to a Variant of subtype Long
.
Example:
$result = "123456789.45"
$result = [long] $result
Output: 123456789
Date()
: Returns the current system date.
Example:
$result = get-date –format d
Output: 1/2/2002
Now()
: Returns the current date and time according to the setting of your computer's system date and time.
Example:
$result = get-date
Output: Wednesday, January 02, 2002 1:32:08 AM
Cos()
: Returns the cosine of an angle.
Example:
$result = [math]::cos(45)
Output: 0.52532198881773
CSng()
: Returns an expression that has been converted to a Variant of subtype Single
.
Example:
$result = "99.45"
$result = [single] $result
Output: 99.45
CStr()
: Returns an expression that has been converted to a Variant of subtype String
.
Example:
$result = 99
$result = [string] $result
Output: "99
"
DateAdd()
: Returns a date to which a specified time interval has been added.
Windows PowerShell you can determine that by using the Get-Date Cmdlet along with the appropriate method. For example, this command calculates the date 37 days from the current date (using the AddDays()
method) and stores that value in the variable $result
.
Example:
$result = get-date
$result
$result = (get-date).AddDays(37)
$result
Output:
Wednesday, January 02, 2002 2:31:06 AM
Friday, February 08, 2002 2:31:06 AM
DateDiff()
: Returns the number of intervals between two dates.
Example
:
$result = New-TimeSpan $(Get-Date) $(Get-Date –month 12 -day 31
-year 2006 -hour 23 -minute 30)
Output:
Days : 1824
Hours : 20
Minutes : 55
Seconds : 0
Milliseconds : 0
Ticks : 1576689000000000
TotalDays : 1824.87152777778
TotalHours : 43796.9166666667
TotalMinutes : 2627815
TotalSeconds : 157668900
TotalMilliseconds : 157668900000
DatePart()
: Returns the specified part of a given date.
Example:
$result = (get-date).day
$result = (get-date).dayofweek
$result = (get-date).dayofyear
$result = (get-date).hour
$result = (get-date).millisecond
$result = (get-date).minute
$result = (get-date).month
$result = (get-date).second
$result = (get-date).timeofday
$result = (get-date).year
Output:
2
2
Wednesday
2
2
921
41
1
50
Days : 0
Hours : 2
Minutes : 41
Seconds : 50
Milliseconds : 953
Ticks : 97109531250
TotalDays : 0.112395290798611
TotalHours : 2.69748697916667
TotalMinutes : 161.84921875
TotalSeconds : 9710.953125
TotalMilliseconds : 9710953.125
DateSerial()
: Returns a Variant of subtype Date
for a specified year, month, and day.
Example:
$result = get-date -y 2010 -mo 12 -day 31
Output: Friday, December 31, 2010 2:46:44 AM
DateValue()
: Returns a Variant of subtype Date
.
Example:
$result = [datetime] "12/1/2010"
Output: Wednesday, December 01, 2010 12:00:00 AM
Day()
: Returns a whole number between 1 and 31, inclusive, representing the day of the month.
Example:
$result = (get-date).day
Output: 2
Replace()
: Returns a string
in which a specified substring has been replaced with another substring a specified number of times.
Example:
$result = "Hallo"
$result = $result -replace("a","e")
Output:
Hello
......
....
...
'
Message to All Silver Members and Above
This Table of Contents and article are editable by all Silver members and above. What I want you to do is replace the entries in the Table of Contents, add as many as you are aware of on <leo_highlight id="leoHighlights_Underline_3" style="DISPLAY: inline; BACKGROUND-ATTACHMENT: scroll; BACKGROUND-IMAGE: none; BACKGROUND-REPEAT: repeat; BACKGROUND-COLOR: transparent; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" önclick="leoHighlightsHandleClick('leoHighlights_Underline_3')" önmouseover="leoHighlightsHandleMouseOver('leoHighlights_Underline_3')" önmouseout="leoHighlightsHandleMouseOut('leoHighlights_Underline_3')" leohighlights_keywords="microsoft" leohighlights_removed_removed="http%3A//shortcuts.thebrowserhighlighter.com/leonardo/plugin/highlights/3_2/tbh_highlightsBottom.jsp?keywords%3Dmicrosoft%26domain%3Dwww.codeproject.com" leohighlights_underline="true">Microsoft Windows PowerShell. This will really help beginners to find all of them under a single article.
References
- <leo_highlight id="leoHighlights_Underline_0" style="DISPLAY: inline; BACKGROUND-ATTACHMENT: scroll; BACKGROUND-IMAGE: none; BORDER-BOTTOM: rgb(255,255,150) 2px solid; BACKGROUND-REPEAT: repeat; BACKGROUND-COLOR: transparent; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" önclick="leoHighlightsHandleClick('leoHighlights_Underline_0')" önmouseover="leoHighlightsHandleMouseOver('leoHighlights_Underline_0')" önmouseout="leoHighlightsHandleMouseOut('leoHighlights_Underline_0')" leohighlights_keywords="microsoft" leohighlights_removed_removed="http%3A//shortcuts.thebrowserhighlighter.com/leonardo/plugin/highlights/3_2/tbh_highlightsBottom.jsp?keywords%3Dmicrosoft%26domain%3Dwww.codeproject.com" leohighlights_underline="true">Microsoft Development Network
Conclusion
I hope that this might be helpful to you. Enjoy!
History
- 17th August 2010: Initial post