Hey everyone, I'm working on a PowerShell script for work that retrieves the NTP server used by Windows Time and checks if it's valid. I'm running into an issue where I'm getting an error message: "The term is not recognized as a cmdlet" when trying to use a variable that contains the server address. The relevant part of my script looks like this:
```powershell
$w32tm_output = & w32tm /query /status
$ntp_source = ($w32tm_output -match "Source:").Replace("Source: ", "")
$ntp_query = & w32tm /stripchart /computer:$ntp_source /dataonly /samples:1
```
When I manually replace `$ntp_source` with the actual server address, the command works fine. I've also tried escaping the `$ntp_source`, but that hasn't resolved the issue. Has anyone faced something similar and how did you fix it? Thanks!
1 Answer
It seems like you're dealing with a bit of a mix-up in your command. The way you're using `-match` to set `$ntp_source` might not give you what you expect. Instead of doing it directly like that, you want to ensure you're actually capturing the correct output before applying `Replace`. Try wrapping it in parentheses like this:
```powershell
$ntp_raw = ($w32tm_output -match "Source:")
$ntp_source = $ntp_raw.Replace("Source: ", "")
```
Also, remember that `-match` behaves differently depending on the type of values on the left side. If it's an array, make sure you’re working with it correctly!

Good point! I had a similar issue before and it messed up my output. Wrapping it in parentheses totally cleared things up for me too. Thanks for sharing!