BCP does not export any records

Hi,

I am running the below powershell script to export some data into a text file:
$DBName = 'MYDB'
#$Srv = '$(ESCAPE_SQUOTE(SRVR))'
$Srv = 'MYServer\MyInstance'
SL SQLSERVER:\SQL\"$Srv"\Databases\"$DBName"\Tables
$bcpconn = '-T'

$IDColumn = 'MyID'

$TableName = 'MyTable'
$ParentTable = 'MyParentTable'

$Query = '"select * from ' + $DBName + '.dbo.' + $TableName + ' WHERE ' + $IDColumn + ' IN (SELECT ' + $IDColumn + ' FROM ' + $DBName + '.dbo.' + $ParentTable + ' WHERE $Partition.WBC_' + $ParentTable + '_pf(PartitionDate) = 2)"'
$Saveas = Invoke-Sqlcmd -query "SELECT [value] FROM [SupportDB].[Support].[PartitioningConfiguration] (nolock) WHERE name = 'ArchivePath' and TableName = '$TableName'"
$ReplaceServername = $Srv -replace '\\', '_'
$Saveas.value = $Saveas.value + $ReplaceServername + "_" + $DBName + "_" + $TableName + '.txt'
bcp $Query QUERYOUT $Saveas.value -n $bcpconn -S $Srv

I get a message: "0 rows copied." and the output file is empty.

When I take the value of the $Query variable and run it in Management Studio, it returns around 150 records.

When I run:
Invoke-Sqlcmd -query $Query

I get:
Invoke-Sqlcmd : The identifier that starts with 'select * from MyDB.dbo.MyTable WHERE MyID IN (SELECT MyID FROM MY' is too long. Maximum length is 128.

Any ideas?

Thanks.