Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Test-Connection

    Sends ICMP echo request packets, or pings, to one or more computers.

    Syntax

    DefaultPing (Default)

    Test-Connection    [-TargetName] <string[]>    [-Ping]    [-IPv4]    [-IPv6]    [-ResolveDestination]    [-Source <string>]    [-MaxHops <int>]    [-Count <int>]    [-Delay <int>]    [-BufferSize <int>]    [-DontFragment]    [-Quiet]    [-TimeoutSeconds <int>]    [<CommonParameters>]

    RepeatPing

    Test-Connection    [-TargetName] <string[]>    -Repeat    [-Ping]    [-IPv4]    [-IPv6]    [-ResolveDestination]    [-Source <string>]    [-MaxHops <int>]    [-Delay <int>]    [-BufferSize <int>]    [-DontFragment]    [-Quiet]    [-TimeoutSeconds <int>]    [<CommonParameters>]

    TraceRoute

    Test-Connection    [-TargetName] <string[]>    -Traceroute    [-IPv4]    [-IPv6]    [-ResolveDestination]    [-Source <string>]    [-MaxHops <int>]    [-Quiet]    [-TimeoutSeconds <int>]    [<CommonParameters>]

    MtuSizeDetect

    Test-Connection    [-TargetName] <string[]>    -MtuSize    [-IPv4]    [-IPv6]    [-ResolveDestination]    [-Quiet]    [-TimeoutSeconds <int>]    [<CommonParameters>]

    TcpPort

    Test-Connection    [-TargetName] <string[]>    -TcpPort <int>    [-IPv4]    [-IPv6]    [-ResolveDestination]    [-Source <string>]    [-Count <int>]    [-Delay <int>]    [-Repeat]    [-Quiet]    [-TimeoutSeconds <int>]    [-Detailed]    [<CommonParameters>]

    Description

    TheTest-Connection cmdlet sends Internet Control Message Protocol (ICMP) echo request packets, orpings, to one or more remote computers and returns the echo response replies. You can use thiscmdlet to determine whether a particular computer can be contacted across an IP network.

    You can use the parameters ofTest-Connection to specify both the sending and receiving computers,to run the command as a background job, to set a time-out and number of pings, and to configure theconnection and authentication.

    Unlike the familiarping command,Test-Connection returns aTestConnectionCommand+PingStatus object that you can investigate in PowerShell. TheQuietparameter returns aBoolean value in aSystem.Boolean object for each tested connection. Ifmultiple connections are tested, an array ofBoolean values is returned.

    Examples

    Example 1: Send echo requests to a remote computer

    This example sends echo request packets from the local computer to the Server01 computer.

    Test-Connection -TargetName Server01 -IPv4
       Destination: Server01Ping Source           Address                   Latency BufferSize Status                                                   (ms)        (B)---- ------           -------                   ------- ---------- ------   1 ADMIN1           10.59.137.44                   24         32 Success   2 ADMIN1           10.59.137.44                   39         32 Success   3 ADMIN1           *                               *          * TimedOut   4 ADMIN1           10.59.137.44                   28         32 Success

    Test-Connection uses theTargetName parameter to specify the Server01 computer. TheIPv4parameter specifies the protocol for the test.

    A series ofTestConnectionCommand+PingStatus objects are sent to the output stream, oneobject per ping reply from the target machine.

    Example 2: Send echo requests to several computers

    This example sends pings from the local computer to several remote computers.

    Test-Connection -TargetName Server01, Server02, Server12

    Example 3: Use parameters to customize the test command

    This example uses the parameters ofTest-Connection to customize the command. The local computersends a ping test to a remote computer.

    Test-Connection -TargetName Server01 -Count 3 -Delay 2 -MaxHops 255 -BufferSize 256

    Test-Connection uses theTargetName parameter to specify Server01. TheCount parameterspecifies three pings are sent to the Server01 computer with aDelay of 2-second intervals.

    You might use these options when the ping response is expected to take longer than usual, eitherbecause of an extended number of hops or a high-traffic network condition.

    Example 4: Run a test as a background job

    This example shows how to run aTest-Connection command as a PowerShell background job.

    $job = Start-Job -ScriptBlock { Test-Connection -TargetName (Get-Content -Path "Servers.txt") }$Results = Receive-Job $job -Wait

    TheStart-Job command uses theTest-Connection cmdlet to ping many computers in an enterprise.The value of theTargetName parameter is aGet-Content command that reads a list of computernames from theServers.txt file. The command uses theStart-Job cmdlet to run the command as abackground job and it saves the job in the$job variable.

    TheReceive-Job command is instructed to-Wait until the job is completed, and then gets theresults and stores them in the$Results variable.

    Example 5: Create a session only if a connection test succeeds

    This example creates a session on the Server01 computer only if at least one of the pings sent tothe computer succeeds.

    if (Test-Connection -TargetName Server01 -Quiet) { New-PSSession -ComputerName Server01 }

    TheTest-Connection cmdlet pings theServer01 computer, with theQuiet parameter provided.The resulting value is$true if any of the four pings succeed. If none of the pings succeed,the value is$false.

    If theTest-Connection command returns a value of$true, the command uses theNew-PSSessioncmdlet to create thePSSession.

    Example 6: Use the Traceroute parameter

    Introduced in PowerShell 6.0, theTraceroute parameter maps a route between the local computerand the remote destination you specify with theTargetName parameter.

    Test-Connection -TargetName www.google.com -Traceroute
       Target: google.comHop Hostname                  Ping Latency Status           Source       TargetAddress                                      (ms)--- --------                  ---- ------- ------           ------       -------------  1 172.20.0.1                   1       4 Success          Lira         172.217.9.174  1 172.20.0.1                   2       3 Success          Lira         172.217.9.174  1 172.20.0.1                   3       2 Success          Lira         172.217.9.174  2 12.108.153.193               1       3 Success          Lira         172.217.9.174  2 12.108.153.193               2       3 Success          Lira         172.217.9.174  2 12.108.153.193               3       2 Success          Lira         172.217.9.174  3 12.244.85.177                1      11 Success          Lira         172.217.9.174  3 12.244.85.177                2      12 Success          Lira         172.217.9.174  3 12.244.85.177                3      12 Success          Lira         172.217.9.174  4 *                            1      14 DestinationNetw… Lira         172.217.9.174  4 *                            2       * TimedOut         Lira         172.217.9.174  4 *                            3      20 DestinationNetw… Lira         172.217.9.174  5 *                            1       * TimedOut         Lira         172.217.9.174  5 *                            2      15 DestinationNetw… Lira         172.217.9.174  5 *                            3       * TimedOut         Lira         172.217.9.174  6 *                            1      18 DestinationNetw… Lira         172.217.9.174  6 *                            2       * TimedOut         Lira         172.217.9.174  6 *                            3      16 DestinationNetw… Lira         172.217.9.174  7 *                            1       * TimedOut         Lira         172.217.9.174  7 *                            2       * TimedOut         Lira         172.217.9.174  7 *                            3       * TimedOut         Lira         172.217.9.174  8 *                            1       * TimedOut         Lira         172.217.9.174  8 *                            2       * TimedOut         Lira         172.217.9.174  8 *                            3       * TimedOut         Lira         172.217.9.174  9 *                            1       * TimedOut         Lira         172.217.9.174  9 *                            2       * TimedOut         Lira         172.217.9.174  9 *                            3       * TimedOut         Lira         172.217.9.174 10 *                            1       * TimedOut         Lira         172.217.9.174 10 *                            2       * TimedOut         Lira         172.217.9.174 10 *                            3       * TimedOut         Lira         172.217.9.174 11 172.217.9.174                1      23 Success          Lira         172.217.9.174 11 172.217.9.174                2      21 Success          Lira         172.217.9.174 11 172.217.9.174                3      22 Success          Lira         172.217.9.174

    TheTest-Connection command is called with theTraceroute parameter. The results, which are[Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus] objects, are output totheSuccess output stream.

    Example 7: Get detailed output for a TCP connection test

    When you use theDetailed parameter, this cmdlet returns a detailed information about the statusof the TCP connection attempts.

    Test-Connection bing.com -TCPPort 443 -Detailed -Count 4
       Target: bing.comId Source     Address           Port Latency(ms) Connected Status-- ------     -------           ---- ----------- --------- ------ 1 circumflex 2620:1ec:c11::200  443          12 True      Success 2 circumflex 2620:1ec:c11::200  443          14 True      Success 3 circumflex 2620:1ec:c11::200  443          17 True      Success 4 circumflex 2620:1ec:c11::200  443          17 True      Success

    Parameters

    -BufferSize

    Specifies the size, in bytes, of the buffer sent with this command. The default value is 32.

    Parameter properties

    Type:Int32
    Default value:32
    Supports wildcards:False
    DontShow:False
    Aliases:Size, Bytes, BS

    Parameter sets

    DefaultPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    RepeatPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Count

    Specifies the number of echo requests to send. The default value is 4.

    Parameter properties

    Type:Int32
    Default value:4
    Supports wildcards:False
    DontShow:False

    Parameter sets

    DefaultPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    TcpPort
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Delay

    Specifies the interval between pings, in seconds.

    Parameter properties

    Type:Int32
    Default value:None
    Supports wildcards:False
    DontShow:False

    Parameter sets

    DefaultPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    RepeatPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    TcpPort
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Detailed

    When you use theDetailed parameter, this cmdlet returns a detailed information about the statusof the TCP connection attempts.

    This parameter was added in PowerShell 7.4.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False

    Parameter sets

    TcpPort
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -DontFragment

    This parameter sets theDon't Fragment flag in the IP header. You can use this parameter withtheBufferSize parameter to test the Path MTU size. For more information about Path MTU, see thePath MTU Discovery article in wikipedia.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False

    Parameter sets

    DefaultPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    RepeatPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -IPv4

    Forces the cmdlet to use the IPv4 protocol for the test.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False

    Parameter sets

    (All)
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -IPv6

    Forces the cmdlet to use the IPv6 protocol for the test.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False

    Parameter sets

    (All)
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -MaxHops

    Sets the maximum number of hops that an ICMP request message can be sent. The default value iscontrolled by the operating system. The default value for Windows 10 and higher is 128 hops.

    Parameter properties

    Type:Int32
    Default value:128
    Supports wildcards:False
    DontShow:False
    Aliases:Ttl, TimeToLive, Hops

    Parameter sets

    DefaultPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    RepeatPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    TraceRoute
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -MtuSize

    This parameter is used to discover the Path MTU size. The cmdlet returns aPingReply#MTUSizeobject that contains the Path MTU size to the target. For more information about Path MTU, see thePath MTU Discovery article in wikipedia.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False
    Aliases:MtuSizeDetect

    Parameter sets

    MtuSizeDetect
    Position:Named
    Mandatory:True
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Ping

    Causes the cmdlet to do a ping test. This is the default mode for theTest-Connection cmdlet.

    Parameter properties

    Type:SwitchParameter
    Default value:True
    Supports wildcards:False
    DontShow:False

    Parameter sets

    DefaultPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    RepeatPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Quiet

    TheQuiet parameter returns aBoolean value. Using this parameter suppresses allerrors.

    Each connection that's tested returns aBoolean value. If theTargetName parameterspecifies multiple computers, an array ofBoolean values is returned.

    Ifany ping to a given target succeeds,$true is returned.

    Ifall pings to a given target fail,$false is returned.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False

    Parameter sets

    (All)
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Repeat

    Causes the cmdlet to send ping requests continuously. When the value ofTargetName is an arrayof targets, the cmdlet repeats the ping requests for the first target only. It ignores theremaining targets. This parameter can't be used with theCount parameter.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False
    Aliases:Continuous

    Parameter sets

    RepeatPing
    Position:Named
    Mandatory:True
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    TcpPort
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -ResolveDestination

    Causes the cmdlet to attempt to resolve the DNS name of the target. When used in conjunctionwith theTraceroute parameter, the DNS names of all intermediate hosts will also beretrieved, if possible.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False

    Parameter sets

    (All)
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Source

    Specifies the names of the computers where the ping originates. Enter a comma-separated list ofcomputer names. The default is the local computer.

    Note

    This parameter is not supported in PowerShell versions 6 and up. Supplying this parametercauses an error.

    Parameter properties

    Type:String
    Default value:Local computer
    Supports wildcards:False
    DontShow:False

    Parameter sets

    DefaultPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    RepeatPing
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    TraceRoute
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False
    TcpPort
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -TargetName

    Specifies the computer(s) to test. Type the computer names or type IP addresses in IPv4 or IPv6format.

    Parameter properties

    Type:

    String[]

    Default value:None
    Supports wildcards:False
    DontShow:False
    Aliases:ComputerName

    Parameter sets

    (All)
    Position:0
    Mandatory:True
    Value from pipeline:True
    Value from pipeline by property name:True
    Value from remaining arguments:False

    -TcpPort

    Specifies the TCP port number on the target to be used in the TCP connection test.

    The cmdlet attempts to make a TCP connection to the specified port on the target.

    • The cmdlet returns$true if a connection is made.
    • The cmdlet returns$false if a connection is not made.

    Parameter properties

    Type:Int32
    Default value:None
    Supports wildcards:False
    DontShow:False

    Parameter sets

    TcpPort
    Position:Named
    Mandatory:True
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -TimeoutSeconds

    Sets the timeout value for the test. The test fails if a response isn't received before the timeoutexpires. The default is five seconds.

    This parameter was introduced in PowerShell 6.0.

    Parameter properties

    Type:Int32
    Default value:5 seconds
    Supports wildcards:False
    DontShow:False

    Parameter sets

    (All)
    Position:Named
    Mandatory:False
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -Traceroute

    Causes the cmdlet to do a traceroute test. When this parameter is used, the cmdlet returns aTestConnectionCommand+TraceStatus object.

    Parameter properties

    Type:SwitchParameter
    Default value:False
    Supports wildcards:False
    DontShow:False

    Parameter sets

    TraceRoute
    Position:Named
    Mandatory:True
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    CommonParameters

    This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,-InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable,-ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, seeabout_CommonParameters.

    Inputs

    None

    You can't pipe objects to this cmdlet.

    Outputs

    Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus

    By default, this cmdlet returns aTestConnectionCommand+PingStatus object for eachping reply.

    Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus

    When you use theTraceroute parameter, this cmdlet returns aTestConnectionCommand+TraceStatus object for each ping reply along the route.

    Boolean

    When you use theQuiet orTcpPort parameters, this cmdlet returns aBoolean value. Ifthe cmdlet tests multiple connections, it returns an array ofBoolean values.

    Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus

    When you use theMtuSize parameter, this cmdlet returns aTestConnectionCommand+PingMtuStatus object for each ping reply.

    Microsoft.PowerShell.Commands.TestConnectionCommand+TcpPortStatus

    When you use theDetailed parameter, this cmdlet returns aTestConnectionCommand+TcpPortStatus object that shows the status of the TCP connection.

    Notes

    On Linux, using theBufferSize parameter or any combination of parameters with theMtuSizeparameter set that results in a non-default buffer size of 32 bytes may requiresudo. In thosecases,Test-Command raises an exception with a message indicating thatsudo is required.

    Related Links

    Collaborate with us on GitHub
    The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

    Feedback

    Was this page helpful?

    YesNoNo

    Need help with this topic?

    Want to try using Ask Learn to clarify or guide you through this topic?

    Suggest a fix?

    In this article

    Was this page helpful?

    YesNo
    NoNeed help with this topic?

    Want to try using Ask Learn to clarify or guide you through this topic?

    Suggest a fix?