[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

powershell

Hi,

I have following script:
#Retrieving the Web Site with List
$MyWeb = Get-SPWeb "http://MySite"

#Get the List name
$MyList = $MyWeb.Lists["MyList"]

#Get The View
#Change the name of the view
$MyView = $MyList.Views["My View"]

#Get the Items from the view
$MyItems = $MyList.GetItems($MyView)

#Get Specific field items and export it to a csv file
$MyItems | %{ select-object -input $_ -prop @{Name='ID';expression={$_.ID;}}, @{Name='Content Type';expression={$_["x. Type"];}}, 
@{Name='Item Name';expression={$_["Item Name"];}}, @{Name='Categories';expression={$_["Categories"];}}, 
@{Name='Item Activity';expression={$_["Item Activity"];}}, 
@{Name='attending';expression={$_["attending"];}}; } | Export-Csv -Path C:\MyExports\export.csv

Open in new window

Now, the categories field is a lookup field how do use substring after function for the look up field so that I get the text after "#" character?
0
Techsavy
Asked:
Techsavy
  • 3
  • 2
1 Solution
 
footechCommented:
I'm not sure if I understand exactly what you're looking for, however try this and see if it fits.  BTW, it's simpler to just pipe to Select-Object, rather than using it within a ForEach so I modified the code to do that.
#Get Specific field items and export it to a csv file
$MyItems | select-object -prop @{Name='ID';expression={$_.ID}},
                                @{Name='Content Type';expression={$_["x. Type"]}},
                                @{Name='Item Name';expression={$_["Item Name"]}},
                                @{Name='Categories';expression={ ($_["Categories"]).substring(($_["Categories"]).IndexOf("#")+1) }},
                                @{Name='Item Activity';expression={$_["Item Activity"]}},
                                @{Name='attending';expression={$_["attending"]}} |
 Export-Csv -Path C:\MyExports\export.csv

Open in new window

0
 
TechsavyAuthor Commented:
Hi footech,

Thank you for the solution. What function I use to get anything before ';#' to get the ID of the LookupList Item? SharePoint Stores lookup field values as 1;#LookupValue so, I need to extract the string before ;# it could be any number.
0
 
footechCommented:
I don't understand what you're asking.  I don't have a environment where I can test this out so you'd need to show me exactly what is being returned now.  Let me know if the code I provided is working for you.  Is this a separate question than your first one, or do you just need a modification of what I provided?
0
 
TechsavyAuthor Commented:
Hi footech.

The solution that you gave me worked for the case where I wanted to return everything after '#'. But now I need to find a way to extract everything that appears before ';#' in a string that is always formatted 12;#somestring. Please note that the number before ;# could be anything from 1 to 20000
0
 
footechCommented:
So the first expression was
($_["Categories"]).substring(($_["Categories"]).IndexOf("#")+1)
To return what's before ";#", you could have
($_["Categories"]).substring(0,($_["Categories"]).IndexOf(";#"))
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now