How to use the command Invoke-Item (with examples)

How to use the command Invoke-Item (with examples)

The Invoke-Item command in PowerShell allows users to open files in their respective default programs. It is a powerful command that can be used to perform various file-related operations through PowerShell.

Use case 1: Open a file in its default program

Code:

Invoke-Item -Path path\to\file

Motivation: This use case is useful when you want to quickly open a specific file in its default program without navigating through the file system manually.

Explanation:

  • -Path: Specifies the path of the file that you want to open. It accepts both absolute and relative file paths.

Example output: If you run the following command: Invoke-Item -Path C:\Documents\example.txt, it will open the file “example.txt” in its default program, such as Notepad.

Use case 2: Open all files inside a directory

Code:

Invoke-Item -Path path\to\directory\*

Motivation: This use case is helpful when you want to open multiple files within a directory without having to individually open each file.

Explanation:

  • -Path: Specifies the path of the directory containing the files. The asterisk (*) is used as a wildcard character to match all files within the directory.

Example output: If you run the command Invoke-Item -Path C:\Documents\*, it will open all files within the “Documents” directory in their respective default programs.

Use case 3: Open all PNGs inside a directory

Code:

Invoke-Item -Path path\to\directory\*.png

Motivation: This use case is beneficial when you specifically want to open all PNG image files within a directory.

Explanation:

  • -Path: Specifies the path of the directory containing the files. The asterisk (*) is used as a wildcard character to match all PNG files within the directory.

Example output: If you run the command Invoke-Item -Path C:\Pictures\*.png, it will open all PNG image files within the “Pictures” directory in their respective default programs.

Use case 4: Open all files inside a directory containing a specific keyword

Code:

Invoke-Item -Path path\to\directory\* -Include *keyword*

Motivation: This use case is useful when you want to open all files within a directory that contain a specific keyword in their file names.

Explanation:

  • -Path: Specifies the path of the directory containing the files. The asterisk (*) is used as a wildcard character to match all files within the directory.
  • -Include: Specifies the keyword to filter the files. The asterisks (*) before and after the keyword are used as wildcard characters to match any characters before and after the keyword in the file names.

Example output: If you run the command Invoke-Item -Path C:\Documents\* -Include *report*, it will open all files within the “Documents” directory that contain the keyword “report” in their file names.

Use case 5: Open all files inside a directory except those containing a specific keyword

Code:

Invoke-Item -Path path\to\directory\* -Exclude *keyword*

Motivation: This use case is helpful when you want to open all files within a directory except those that contain a certain keyword in their names.

Explanation:

  • -Path: Specifies the path of the directory containing the files. The asterisk (*) is used as a wildcard character to match all files within the directory.
  • -Exclude: Specifies the keyword to exclude from the files. The asterisks (*) before and after the keyword are used as wildcard characters to match any characters before and after the keyword in the file names.

Example output: If you run the command Invoke-Item -Path C:\Documents\* -Exclude *draft*, it will open all files within the “Documents” directory except those that contain the keyword “draft” in their file names.

Use case 6: Perform a dry run to determine which files will be opened inside a directory through Invoke-Item

Code:

Invoke-Item -Path path\to\directory\* -WhatIf

Motivation: This use case is useful when you want to preview which files will be opened inside a directory without actually performing the action.

Explanation:

  • -Path: Specifies the path of the directory containing the files. The asterisk (*) is used as a wildcard character to match all files within the directory.
  • -WhatIf: Displays a list of files that would be opened without actually opening them.

Example output: If you run the command Invoke-Item -Path C:\Documents\* -WhatIf, it will display a list of files within the “Documents” directory that would be opened if the command was executed without the -WhatIf parameter.

Related Posts

How to use the command 'snyk' (with examples)

How to use the command 'snyk' (with examples)

Description: The ‘snyk’ command is used to find vulnerabilities in your code and remediate risks.

Read More
How to use the command 'podman rmi' (with examples)

How to use the command 'podman rmi' (with examples)

This article provides a guide on how to use the ‘podman rmi’ command with various examples.

Read More
How to use the command 'telnet' (with examples)

How to use the command 'telnet' (with examples)

Telnet is a command-line tool that allows users to connect to a remote host over a network using the telnet protocol.

Read More