Category Archives: Host

Write-Host

NAME
    Write-Host

SYNOPSIS
    Writes customized output to a host.

SYNTAX
    Write-Host [[-Object] <Object>] [-BackgroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}] [-ForegroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}] [-NoNewline] [-Separator <Object>] [<CommonParameters>]

DESCRIPTION
    The Write-Host cmdlet customizes output. You can specify the color of text by using the ForegroundColor parameter, and you can specify the background color by using the BackgroundColor parameter. The Separator parameter lets you specify a string to use to separate displayed objects. The particular result depends on the program that is hosting Windows PowerShell.

PARAMETERS
    -BackgroundColor <ConsoleColor>
        Specifies the background color. There is no default.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ForegroundColor <ConsoleColor>
        Specifies the text color. There is no default.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -NoNewline [<SwitchParameter>]
        Specifies that the content displayed in the console does not end with a newline character.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Object <Object>
        Objects to display in the console.

        Required?                    false
        Position?                    1
        Default value                None
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Separator <Object>
        String to the output between objects displayed on the console.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Object
        You can pipe objects to be written to the host.

OUTPUTS
    None
        Write-Host sends the objects to the host. It does not return any objects. However, the host might display the objects that Write-Host sends to it.

NOTES

    ————————– EXAMPLE 1 ————————–

    C:\PS>Write-Host “no newline test ” -NoNewline

    no newline test C:\PS>

    Description
    ———–
    This command displays the input to the console, but because of the NoNewline parameter, the output is followed directly by the prompt.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Write-Host (2,4,6,8,10,12) -Separator “, +2= ”

    2, +2= 4, +2= 6, +2= 8, +2= 10, +2= 12

    Description
    ———–
    This command displays the even numbers from 2 through 12. The Separator parameter is used to add the string , +2= (comma, space, +, 2, =, space).

    ————————– EXAMPLE 3 ————————–

    C:\PS>Write-Host (2,4,6,8,10,12) -Separator “, -> ” -ForegroundColor DarkGreen -BackgroundColor white

    Description
    ———–
    This command displays the even numbers from 2 through 12. It uses the ForegroundColor parameter to output dark green text and the BackgroundColor parameter to display a white background.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Write-Host “Red on white text.” -ForegroundColor red -BackgroundColor white

    Red on white text.

    Description
    ———–
    This command displays the string “Red on white text.” The text is red, as defined by the ForegroundColor parameter. The background is white, as defined by the BackgroundColor parameter.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113426
    Write-Verbose
    Write-Error
    Write-Progress
    Write-Debug
    Write-Output
    Write-Warning
    Out-Host

Out-Host

NAME
    Out-Host

SYNOPSIS
    Sends output to the command line.

SYNTAX
    Out-Host [-InputObject <psobject>] [-Paging] [<CommonParameters>]

DESCRIPTION
    The Out-Host cmdlet sends output to the Windows PowerShell host for display. The host displays the output at the command line. Because Out-Host is the default, you do not need to specify it unless you want to use its parameters to change the display.

PARAMETERS
    -InputObject <psobject>
        Specifies the objects that are written to the console. Enter a Variable that contains the objects, or type a command or expression that gets the objects.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Paging [<SwitchParameter>]
        Displays one page of output at a time, and waits for user input before displaying the remaining pages, much like the traditional “more” command. By default, all of the output is displayed on a single page. The page size is determined by the characteristics of the host.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSObject
        You can pipe any object to Out-Host.

OUTPUTS
    None
        Out-Host does not generate any output. However, the host might display the objects that Out-Host sends to it.

NOTES

        You can also refer to Out-Host by its built-in Alias, “oh”. For more information, see about_aliases.

        The cmdlets that contain the Out verb (the Out cmdlets) do not format objects; they just render them and send them to the specified display destination. If you send an unformatted object to an Out cmdlet, the cmdlet sends it to a formatting cmdlet before rendering it.

        The Out cmdlets do not have parameters for names or file paths. To send data to an Out cmdlet, use a pipeline operator (|) to send the output of a Windows PowerShell command to the cmdlet. You can also store data in a Variable and use the InputObject parameter to pass the data to the cmdlet. For help, see the examples.

        Out-Host sends data, but it does not emit any output objects. If you pipe the output of Out-Host to Get-Member, Get-Member reports that no objects have been specified.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-Process | Out-Host -Paging

    Description
    ———–
    This command displays the processes on the system one page at a time. It uses the Get-Process command to get the processes on the system. The pipeline operator (|) sends the results to Out-Host, which displays them at the console. The Paging parameter displays one page of data at a time.

    The same command format is used for the Help Function that is built into Windows PowerShell. That Function gets data from Get-Help and then uses the Paging parameter of Out-Host to display the data one page at a time by using this command format: Get-Help $args[0] | Out-Host -Paging).

    ————————– EXAMPLE 2 ————————–

    C:\PS>$a = Get-History

    C:\PS>Out-Host -InputObject $a

    Description
    ———–
    These commands display the session history at the command line. The first command uses the Get-History cmdlet to get the session history, and then it stores the history in the $a Variable. The second command uses Out-Host to display the content of the $a Variable, and it uses the InputObject parameter to specify the Variable to Out-Host.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113365
    Out-Printer
    Out-Null
    Out-File
    Out-String
    Out-Default
    Write-Host

Read-Host

NAME
    Read-Host

SYNOPSIS
    Reads a line of input from the console.

SYNTAX
    Read-Host [[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]

DESCRIPTION
    The Read-Host cmdlet reads a line of input from the console. You can use it to prompt a user for input. Because you can save the input as a secure string, you can use this cmdlet to prompt users for secure data, such as passwords, as well as shared data.

PARAMETERS
    -AsSecureString [<SwitchParameter>]
        Displays asterisks (*) in place of the characters that the user types as input.

        When you use this parameter, the output of the Read-Host cmdlet is a SecureString object (System.Security.SecureString).

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Prompt <Object>
        Specifies the text of the prompt. Type a string. If the string includes spaces, enclose it in quotation marks. For example, “Type an integer: “.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    System.String or System.Security.SecureString
        If the AsSecureString parameter is used, Read-Host returns a SecureString. Otherwise, it returns a string.

NOTES

    ————————– EXAMPLE 1 ————————–

    C:\PS>$age = Read-Host “Please enter your age:”

    Description
    ———–
    This command displays the string “Please enter your age:” as a prompt. When a value is entered and the Enter key is pressed, the value is stored in the $age Variable.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$pwd_secure_string = Read-Host “Enter a Password:” -AsSecureString

    Description
    ———–
    This command displays the string “Enter a Password:” as a prompt. As a value is being entered, asterisks (*) appear on the console in place of the input. When the Enter key is pressed, the value is stored as a SecureString object in the $pwd_secure_string Variable.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113371
    Get-Host
    Out-Host
    Write-Host
    ConvertFrom-SecureString

Get-Host

NAME
    Get-Host

SYNOPSIS
    Gets an object that represents the current host program. And, displays Windows PowerShell version and regional information by default.

SYNTAX
    Get-Host [<CommonParameters>]

DESCRIPTION
    The Get-Host cmdlet gets an object that represents the program that is hosting Windows PowerShell.

    The default display includes the Windows PowerShell version number and the current region and language settings that the host is using, but the host object contains a wealth of information, including detailed information about the version of Windows PowerShell that is currently running and the current culture and UI culture of Windows PowerShell. You can also use this cmdlet to customize features of the host program user interface, such as the text and background colors.

PARAMETERS
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    System.Management.Automation.Internal.Host.InternalHost
        Get-Host returns a System.Management.Automation.Internal.Host.InternalHost object.

NOTES

        The $host automatic Variable contains the same object that Get-Host returns, and you can use it in the same way. Similarly, the $PSCulture and $PSUICulture automatic Variables contain the same objects that the CurrentCulture and CurrentUICulture properties of the host object contain. You can use these features interchangeably.

        For more information, see about_Automatic_Variables.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-Host

    Name             : ConsoleHost
    Version         : 2.0
    InstanceId     : e4e0ab54-cc5e-4261-9117-4081f20ce7a2
    UI             : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture : en-US
    CurrentUICulture : en-US
    PrivateData     : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    IsRunspacePushed : False
    Runspace         : System.Management.Automation.Runspaces.LocalRunspace

    Description
    ———–
    This command displays information about the Windows PowerShell console, which is the current host program for Windows PowerShell in this example. It includes the name of the host, the version of Windows PowerShell that is running in the host, and current culture and UI culture.

    The Version, UI, CurrentCulture, CurrentUICulture, PrivateData, and Runspace properties each contain an object with very useful properties. Later examples examine these properties.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$h = Get-Host

    C:\PS> $win = $h.ui.rawui.windowsize

    C:\PS> $win.height = 10

    C:\PS> $win.width = 10

    C:\PS> $h.ui.rawui.set_windowsize($win)

    Description
    ———–
    This command resizes the Windows PowerShell window to 10 pixels by 10 pixels.

    ————————– EXAMPLE 3 ————————–

    C:\PS>(Get-Host).version | Format-List -property *

    Major         : 2
    Minor         : 0
    Build         : -1
    Revision     : -1
    MajorRevision : -1
    MinorRevision : -1

    Description
    ———–
    This command gets detailed information about the version of Windows PowerShell running in the host. You can view, but not change, these values.

    The Version property of Get-Host contains a System.Version object. This command uses a pipeline operator (|) to send the version object to the Format-List cmdlet. The Format-List command uses the Property parameter with a value of all (*) to display all of the properties and property values of the version object.

    ————————– EXAMPLE 4 ————————–

    C:\PS>(Get-Host).currentculture | Format-List -property *

    Parent                         : en
    LCID                         : 1033
    KeyboardLayoutId             : 1033
    Name                         : en-US
    IetfLanguageTag                : en-US
    DisplayName                    : English (United States)
    NativeName                     : English (United States)
    EnglishName                    : English (United States)
    TwoLetterISOLanguageName     : en
    ThreeLetterISOLanguageName     : eng
    ThreeLetterWindowsLanguageName : ENU
    CompareInfo                    : CompareInfo – 1033
    TextInfo                     : TextInfo – 1033
    IsNeutralCulture             : False
    CultureTypes                 : SpecificCultures, InstalledWin32Cultures, FrameworkCultures
    NumberFormat                 : System.Globalization.NumberFormatInfo
    DateTimeFormat                 : System.Globalization.DateTimeFormatInfo
    Calendar                     : System.Globalization.GregorianCalendar
    OptionalCalendars             : {System.Globalization.GregorianCalendar, System.Globalization.GregorianCalendar}
    UseUserOverride                : True
    IsReadOnly                     : False

    Description
    ———–
    This command gets detailed information about the current culture set for Windows PowerShell running in the host. This is the same information that is returned by the Get-Culture cmdlet.

    (Similarly, the CurrentUICulture property returns the same object that Get-UICulture returns.)

    The CurrentCulture property of the host object contains a System.Globalization.CultureInfo object. This command uses a pipeline operator (|) to send the CultureInfo object to the Format-List cmdlet. The Format-List command uses the Property parameter with a value of all (*) to display all of the properties and property values of the CultureInfo object.

    ————————– EXAMPLE 5 ————————–

    C:\PS>(Get-Host).currentculture.DateTimeFormat | Format-List -property *

    AMDesignator                     : AM
    Calendar                         : System.Globalization.GregorianCalendar
    DateSeparator                    : /
    FirstDayOfWeek                 : Sunday
    CalendarWeekRule                 : FirstDay
    FullDateTimePattern             : dddd, MMMM dd, yyyy h:mm:ss tt
    LongDatePattern                 : dddd, MMMM dd, yyyy
    LongTimePattern                 : h:mm:ss tt
    MonthDayPattern                 : MMMM dd
    PMDesignator                     : PM
    RFC1123Pattern                 : ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’
    ShortDatePattern                 : M/d/yyyy
    ShortTimePattern                 : h:mm tt
    SortableDateTimePattern         : yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss
    TimeSeparator                    : :
    UniversalSortableDateTimePattern : yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’
    YearMonthPattern                 : MMMM, yyyy
    AbbreviatedDayNames             : {Sun, Mon, Tue, Wed…}
    ShortestDayNames                 : {Su, Mo, Tu, We…}
    DayNames                         : {Sunday, Monday, Tuesday, Wednesday…}
    AbbreviatedMonthNames            : {Jan, Feb, Mar, Apr…}
    MonthNames                     : {January, February, March, April…}
    IsReadOnly                     : False
    NativeCalendarName             : Gregorian Calendar
    AbbreviatedMonthGenitiveNames    : {Jan, Feb, Mar, Apr…}
    MonthGenitiveNames             : {January, February, March, April…}

    Description
    ———–
    This command returns detailed information about the DateTimeFormat of the current culture that is being used for Windows PowerShell.

    The CurrentCulture property of the host object contains a CultureInfo object that, in turn, has many useful properties. Among them, the DateTimeFormat property contains a DateTimeFormatInfo object with many useful properties.

    To find the type of an object that is stored in an object property, use the Get-Member cmdlet. To display the property values of the object, use the Format-List cmdlet.

    ————————– EXAMPLE 6 ————————–

    C:\PS>(Get-Host).ui.rawui | Format-List -property *

    ForegroundColor     : DarkYellow
    BackgroundColor     : DarkBlue
    CursorPosition        : 0,390
    WindowPosition        : 0,341
    CursorSize            : 25
    BufferSize            : 120,3000
    WindowSize            : 120,50
    MaxWindowSize         : 120,81
    MaxPhysicalWindowSize : 182,81
    KeyAvailable         : False
    WindowTitle         : Windows PowerShell 2.0 (04/11/2008 00:08:14)

    Description
    ———–
    This command displays the properties of the RawUI property of the host object. By changing these values, you can change the appearance of the host program.

    ————————– EXAMPLE 7 ————————–

    C:\PS>(Get-Host).ui.rawui.backgroundcolor = “Black”

    C:\PS> cls

    Description
    ———–
    These commands change the background color of the Windows PowerShell console to black. The “cls” command is an Alias for the Clear-Host Function, which clears the screen and changes the whole screen to the new color.

    This change is effective only in the current session. To change the background color of the console for all sessions, add the command to your Windows PowerShell profile.

    ————————– EXAMPLE 8 ————————–

    C:\PS>$host.privatedata.errorbackgroundcolor = “white”

    Description
    ———–
    This command changes the background color of error messages to white.

    This command uses the $host automatic Variable, which contains the host object for the current host program. Get-Host returns the same object that $host contains, so you can use them interchangeably.

    This command uses the PrivateData property of $host as its ErrorBackgroundColor property. To see all of the properties of the object in the $host.privatedata property, type “$host.privatedata | Format-List * “.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113318
    Read-Host
    Out-Host
    Write-Host