You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Replaces matching text (literal or regex) within optional line range. Preserves file metadata (encoding, newlines). Use -Contains for literal string replacement or -Pattern for regex-based replacement with capture groups support.
30
+
Replaces matching text (literal or regex) within optional line range. Preserves file metadata (encoding, newlines). Use -OldText for literal string replacement or -Pattern for regex-based replacement with capture groups support.
Update-MatchInFile code.cs -Pattern "var (\w+)" -Replacement "string $1" # Regex with capture groups
38
38
```
39
39
@@ -69,21 +69,6 @@ Accept pipeline input: False
69
69
Accept wildcard characters: False
70
70
```
71
71
72
-
### -Contains
73
-
Specifies a literal string to match in lines. Only lines containing this substring will be processed for replacement. Unlike -Pattern (which uses regex), -Contains performs simple substring matching without interpreting special characters. This is useful when filtering lines that contain regex metacharacters like '[', ']', '(', ')', '.', '*', '+', '?', ' ' without needing to escape them. When used with -Replacement but without -Pattern, the entire line containing the string is replaced.
74
-
75
-
```yaml
76
-
Type: String
77
-
Parameter Sets: (All)
78
-
Aliases:
79
-
80
-
Required: False
81
-
Position: Named
82
-
Default value: None
83
-
Accept pipeline input: False
84
-
Accept wildcard characters: False
85
-
```
86
-
87
72
### -Encoding
88
73
Specifies the character encoding. If omitted, encoding is auto-detected and preserved. Supports: utf8, utf8bom, sjis, eucjp, jis, ascii, utf16, utf32, etc.
89
74
@@ -205,6 +190,21 @@ Accept pipeline input: False
205
190
Accept wildcard characters: False
206
191
```
207
192
193
+
### -OldText
194
+
Literal string to search for and replace. Use with -Replacement parameter. The matched text (not the entire line) is replaced. For regex-based replacement, use -Pattern instead.
195
+
196
+
```yaml
197
+
Type: String
198
+
Parameter Sets: (All)
199
+
Aliases:
200
+
201
+
Required: False
202
+
Position: Named
203
+
Default value: None
204
+
Accept pipeline input: False
205
+
Accept wildcard characters: False
206
+
```
207
+
208
208
### CommonParameters
209
209
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
210
210
@@ -222,14 +222,14 @@ BEST PRACTICE - Verify before and after:
222
222
223
223
1. Check current content: Show-TextFile config.js -Pattern "localhost"
224
224
225
-
2. Make the replacement: Update-MatchInFile config.js -Contains "localhost" -Replacement "production"
225
+
2. Make the replacement: Update-MatchInFile config.js -OldText "localhost" -Replacement "production"
226
226
227
227
3. Verify the change: Show-TextFile config.js -Pattern "production"
228
228
229
229
CRITICAL - Handling special characters (dollar, braces, quotes):
230
230
231
231
When replacing code with special characters, ALWAYS use here-strings.
232
-
Example: $old = @'...'@ and $new = @'...'@ then Update-MatchInFile file.cs -Contains $old -Replacement $new
232
+
Example: $old = @'...'@ and $new = @'...'@ then Update-MatchInFile file.cs -OldText $old -Replacement $new
233
233
234
234
Here-strings (@'...'@) treat ALL characters literally.
0 commit comments