We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

How to specify a string in powershell that has spaces in it.

48 Views
Last Modified: 2021-03-22
Dear Experts,

I have a powershell-script that backup my Sharepoint Lists. In the code you also have to specify the columns of that list. For the column "Date" there are no spaces in the name. So this is the code:

$ExportItem | Add-Member -MemberType NoteProperty -name "Date" -value $_["Date"].ToLocalTime().ToString("dd-MM-yyyy");
But the other columns has spaces in their names, so i use the column-id. Because i do not know how to retreive the column-name when there are spaces in it.

$ExportItem | Add-Member -MemberType NoteProperty -name "00:00 - 07:59" -value $_["_x0030_0_x003a_00_x0020__x002d__"]
How can i retreive the column-name in my powershell-code when there are spaces in the column-name?
This is the whole code:
$list = $Context.web.Lists.GetByTitle(Mylist")
$view = $list.views.GetByTitle("test")  
$context.load($view)  
$context.executeQuery()  
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View><Query>" + $view.ViewQuery +"</Query></View>"
$ListItems = $List.GetItems($Query)
$context.Load($ListItems)
$context.ExecuteQuery()
$ListItemCollection = @() 
$ListItems | foreach {
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -name "Date" -value $_["Date"].ToLocalTime().ToString("dd-MM-yyyy");
$ExportItem | Add-Member -MemberType NoteProperty -name "00:00 - 07:59" -value $_["_x0030_0_x003a_00_x0020__x002d__"]  
$ExportItem | Add-Member -MemberType NoteProperty -name "08:00 - 16:59" -value $_["_x0030_8_x003a_00_x0020__x002d__"]  
$ExportItem | Add-Member -MemberType NoteProperty -name "17:00 - 23:59" -value $_["_x0031_7_x003a_00_x0020__x002d__"]  
$ExportItem | Add-Member -MemberType NoteProperty -name "Opmerkingen" -value $_["Opmerkingen"]
$ListItemCollection += $ExportItem
}
$ListItemCollection | Export-XLSX -Path "c:\Mylist.xlsx" -Table -Autofit -Force
Comment
Watch Question
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE