# Refactoring

## About

Refactorings allow you to change or generate code based on the code you have. You will find a list of refactors below. You can invoke a refactor by invoke the Refactor command or by pressing the key binding `Ctrl+Alt+R` .&#x20;

Only valid refactors will be returned in the drop down menu.

## Convert to $\_

This refactoring converts a `$PSItem` variable to the `$_` variable.&#x20;

![Convert to $\_](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MPXNbhKjVqiTlhr4AWh%2F-MPXO04v5yHzXhiR1aTm%2Fconverttodollarunder.gif?alt=media\&token=eb8f06a5-eb30-4891-8b74-f42fcbd10138)

## Convert to $PSItem

Converts a reference to the `$_` variable to `$PSItem`.

![Convert to $PSItem](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MPXNbhKjVqiTlhr4AWh%2F-MPXOJpL1Oup_fK-FvSh%2Fconverttopsitem.gif?alt=media\&token=6cb58699-e9fb-4345-a1fb-39dc67a55175)

## Convert to Multiline Command

Converts a command invocation into a multi-line command. Each parameter and argument is broken up with backticks.&#x20;

![](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MOZRXr5WM1oozM7xgJV%2F-MOZS9SXQsx48tayR16e%2Fmulti-line.gif?alt=media\&token=8aab2a88-85bc-4a4d-94b9-0270edbd0f21)

## Convert to Splat

Converts a command invocation into a splatting expression and creates a hashtable named `$Parameters` and then passes that hashtable as a splatting expression to the command. Positional arguments are not added to the hashtable.

![Convert to Splat](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MOU6iPzGXgBkpIjnghK%2F-MOUGowA5Q-fn30u2UTF%2Fconvert-to-splat.gif?alt=media\&token=59aadc2c-d149-4c92-bbf8-2f3601664bb3)

## Export Module Member

Exports the selected variable or function from a module using `Export-ModuleMember`.

![Export Module Member](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MOTs49qXGdqNpZ6Ywez%2F-MOU6cJ8KcTt9uMTBq7s%2Fexport-module-member.gif?alt=media\&token=b3be4a3e-f1b9-44a9-b468-7e77d4d8404b)

## Extract Function

You can use the Extract Function refactor to convert a section of code into a function. It will analyze the selected block and determine if there are variables that should be added as parameters. These variables will be added to the `param` block.&#x20;

![Extract Function](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MOUfrWXgiPeun0CyZ-f%2F-MOUgHXdNw7fZnFH_Z1v%2Fexport-function.gif?alt=media\&token=feb8c562-daf9-4dc3-982d-ea1728ca5b97)

## Extract Selection to File

You can use the Extract Selection to File refactor to create a new file based on the selection in the current active editor.&#x20;

![Extract Selection to File](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MOTrNxvTCnmuoFnoQcL%2F-MOTrwRfBGcOtWZdOcM_%2Fextract-file.gif?alt=media\&token=02a6e024-d527-474c-a899-24705563aa03)

## Generate Function from Usage

You can generate a function based on a command example. This refactoring will analyze the parameters, arguments and whether the command is used in a pipeline. If used in a pipeline, this refactoring will generate an advanced function.&#x20;

![Generate Function from Usage](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MOniVKODpgMVe1cJGa2%2F-MOnjGH6ICKfhLoAMKdW%2Fgenerate-function.gif?alt=media\&token=f6d1b828-4939-4f63-9889-db4029c726e9)

## Generate Proxy Function

Proxy functions allow you to extend existing functions with new parameters and functionality. You can select a command that you use within your script and select the Generate Proxy Function refactoring to have it generate the proxy function code for you.

![Generate Proxy Function](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MPsHjJG6X4qsp0HYexm%2F-MPsICFqK3dXdKAczbda%2Fproxy.gif?alt=media\&token=79290e22-fae3-4ea6-b9bc-557a19f180f6)

## Introduce Using Namespace

The introduce using namespace refactoring adds a `using namespace` statement to the top of a script and replaces the selected type expression with the namespace removed.&#x20;

![Introduce Using Namespace](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MPSBxhqDCF6kJS3S6xn%2F-MPSCRt_mo2RtJDZ8U63%2Fintroduce-using.gif?alt=media\&token=0e15d1e5-fd5f-404f-a3e2-d3c151c7d1a7)

## Reorder Parameters

You can reorder parameters by using the `Ctrl+PageUp` and `Ctrl+PageDown` key bindings. Ensure that your cursor is on top of a parameter for a command. Press one of the key bindings. To move a parameter to the right, use Page Up. To move a parameter to the left, use Page Down.&#x20;

![Reorder Parameters](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MOsQpeCGJN7K6kIG_6K%2F-MOsRAZOJjGKyJVvLHSZ%2Fmove-parameters.gif?alt=media\&token=128dd386-39ac-4b1b-9e5c-4fae18013dc5)

## Split Pipeline

The split pipe refactoring will split a pipe into multiple lines. Each element in the pipe is stored in a variable and passed to the next item in the pipe. This can be useful for debugging long or complex pipeline operations.

![Split pipeline](https://3667946160-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LNFE66tpE_51uobNA70%2F-MPRtE0c_spmE9BnQb84%2F-MPRuUr-DpGW2sRPuBs1%2Fsplit-pipe.gif?alt=media\&token=97c2f81a-7474-44f4-82f2-fc0d703e13de)
