Try something like this to use the stream option instead of trying to interpret CIM etc...
$IPConfig = get-netIPConfiguration | Out-String -Stream
ForEach($item in $IPConfig) {
If($item.ToString() -like '*DefaultGateway*') {
$defaultGateway = $item.Replace('IPv4DefaultGateway : ', '')
}
}
write-host $defaultGateway