Converting VBScript's CStr Function

Definition: Returns an expression that has been converted to a Variant of subtype String.


There will be times (especially when you work with databases) when you’ll have a numeric value that needs to be treated as a string. Can you convert a numeric value into a string value? Of course you can. To begin with, assign the numeric value to a variable (in this example, we assign 17 to $a):

$a = 17

If we want to verify that $a is really a numeric value we can do this using the GetType() method:

IsPublic IsSerial Name
-------- -------- ----
True     True     Int32

Now, what about converting this to a string value? Here you go:

$a = [string] $a

When you run this command and then check the data type of $a you should get the following:

IsPublic IsSerial Name
-------- -------- ----
True     True     String

Return to the VBScript to Windows PowerShell home page