Here is a map of the Swiss rail system so you can see the possible train change nnections from St. Moritz:
https://www.sbb.ch/content/dam/sbb/de/pdf/freizeit-ferien/ferien-kurztrips-schweiz/internationale-gaeste/sts-geltungsbereich.pdf
Bahn.de shows a trip of 12-13 hours going from St. Moritz north to Chur, thence east across Austria to pick up the EC 113/213 which connects Munich and Ljubljana, or north east to Munich to pick up the EC train there.
If you have two days for the trip, you could consider a very scenic route, south from St. Moritz over the Bernina Pass into Italy, overnighting at Varenna or Milan ( I would choose Varenna), then continuing the next day via Venice Mestre to Trieste, where you take a bus the last bit to Ljubljana ( it is not far).
If you have three days, maybe spend the second night in Trieste.